示例#1
0
        public override async Task WriteAsync(RedisConvert convert, Type objectType, object value, Namespace name, TimeSpan?TTL = null)
        {
            //Convert to string and then write it to the converter
            await convert.WriteValueAsync(name, value.ToString(), TTL);

            return;
        }
示例#2
0
        public override async Task <object> ReadAsync(RedisConvert convert, Type objectType, object existingValue, Namespace name)
        {
            //Make sure the type is valid.
            var types = objectType.GetGenericArguments();

            Debug.Assert(types.Length == 1);

            if (!types[0].IsPrimitive && !types[0].IsEnum)
            {
                throw new ArgumentException("HashSet type is invalid. Only primitive HashSets are supported.", "value");
            }

            //Get the hashset
            HashSet <string> hashset = await convert.Client.FetchHashSetAsync(name.Build());

            //Prepare a hashset of the new type
            var existingSet = existingValue;

            if (existingValue == null)
            {
                existingSet = Activator.CreateInstance(objectType);
            }

            //Prepare the add function
            var add = objectType.GetMethod("Add");

            //Iterate over every value in the hashset adding it to the new hashset
            foreach (var s in hashset)
            {
                object val = TypeDescriptor.GetConverter(types[0]).ConvertFromString(s);
                add.Invoke(existingSet, new object[] { val });
            }

            return(existingSet);
        }
示例#3
0
        public override async Task WriteAsync(RedisConvert convert, Type objectType, object value, Namespace name, TimeSpan?TTL = null)
        {
            //Prepare the options
            RedisOptionAttribute options = objectType.GetCustomAttribute <RedisOptionAttribute>();

            if (options == null)
            {
                options = new RedisOptionAttribute();
            }

            //Store it as a simple string
            if (options.SingleValueKey)
            {
                await convert.Client.StoreStringAsync(name.Build(), value.ToString(), TTL);

                return;
            }

            //The content that will be written to the HashMap in bulk
            Dictionary <string, string> hashmap = new Dictionary <string, string>();

            //Iterate over every property
            foreach (var property in objectType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                var ignoreAttribute = property.GetCustomAttribute <RedisIgnoreAttribute>();
                if (ignoreAttribute != null)
                {
                    continue;
                }

                //Prepare some property values
                var propertyName      = property.Name;
                var propertyAttribute = property.GetCustomAttribute <RedisPropertyAttribute>();
                if (propertyAttribute != null)
                {
                    propertyName = propertyAttribute.DisplayName ?? property.Name;
                }

                //Prepare the serializer
                var serializer = convert.GetSerializerForType(property.PropertyType, property: propertyAttribute);
                Debug.Assert(serializer != null);

                //Push the name and then serialize it
                name.Push(propertyName);
                convert.SetHashMapBuffer(hashmap);
                await serializer.WriteAsync(convert, property.PropertyType, property.GetValue(value), name, TTL);

                name.Pop();
            }

            //Flush our buffer
            convert.SetHashMapBuffer(hashmap);
            await convert.FlushHashMapAsync(name, TTL);
        }
示例#4
0
        public override async Task <object> ReadAsync(RedisConvert convert, Type objectType, object existingValue, Namespace name)
        {
            //Get the current string value
            string value = await convert.Client.FetchStringAsync(name.Build(), null);

            if (value == null)
            {
                return(existingValue);
            }

            //If we are a primitive or enum then convert it from string and return the value
            if (objectType.IsPrimitive || objectType.IsEnum)
            {
                return(TypeDescriptor.GetConverter(objectType).ConvertFromString(value));
            }

            //Return the raw string value.
            return(value);
        }
示例#5
0
        public override async Task WriteAsync(RedisConvert convert, Type objectType, object value, Namespace name, TimeSpan?TTL = null)
        {
            //Prepare the map. If we are already the correct type just use the raw value,
            // otherwise we will convert it to the correct type.
            Dictionary <string, string> map = null;

            if (objectType == typeof(Dictionary <string, string>))
            {
                map = (Dictionary <string, string>)value;
            }
            else
            {
                //Get the generic types and make sure they are valid
                var genericTypes = objectType.GetGenericArguments();
                Debug.Assert(genericTypes.Length == 2);

                if ((!genericTypes[0].IsPrimitive && !genericTypes[0].IsEnum) || (!genericTypes[1].IsPrimitive && !genericTypes[1].IsEnum))
                {
                    throw new ArgumentException("Dictionary type is invalid. Only primitive Dictionaries are supported.", "value");
                }

                //Get the enumerator for the dictionary ( value.GetEnumerator(); )
                var getEnumerator = objectType.GetMethod("GetEnumerator");
                var enumerator    = (IEnumerator)getEnumerator.Invoke(value, null);

                //Get the properties of the keypairs
                var keypairType   = typeof(KeyValuePair <,>).MakeGenericType(genericTypes);
                var keyProperty   = keypairType.GetProperty("Key");
                var valueProperty = keypairType.GetProperty("Value");

                //Iterate over every item and add them to the map
                while (enumerator.MoveNext())
                {
                    var kp_key   = keyProperty.GetValue(enumerator.Current);
                    var kp_value = valueProperty.GetValue(enumerator.Current);
                    map[kp_key.ToString()] = kp_value.ToString();
                }
            }

            //Store the map in Redis
            await convert.WriteHashMapAsync(name, map, TTL);
        }
示例#6
0
        public override async Task WriteAsync(RedisConvert convert, Type objectType, object value, Namespace name, TimeSpan?TTL = null)
        {
            //Prepare the hashset we will copy into
            HashSet <string> hashset = null;

            if (objectType == typeof(HashSet <string>))
            {
                hashset = (HashSet <string>)value;
            }
            else
            {
                var types = objectType.GetGenericArguments();
                Debug.Assert(types.Length == 1);

                //Make sure the type is valid.
                if (!types[0].IsPrimitive && !types[0].IsEnum)
                {
                    throw new ArgumentException("HashSet type is invalid. Only primitive HashSets are supported.", "value");
                }

                //Prepare the set
                hashset = new HashSet <string>();

                //Fetch the enumerator
                var getEnumerator = objectType.GetMethod("GetEnumerator");
                var enumerator    = (IEnumerator)getEnumerator.Invoke(value, null);

                //Iterate over every element converting them to a string and adding them to our set
                while (enumerator.MoveNext())
                {
                    hashset.Add(enumerator.Current.ToString());
                }
            }

            //Write to the DB
            await convert.WriteHashSetAsync(name, hashset, TTL, true);
        }
示例#7
0
 public override Task <object> ReadAsync(RedisConvert convert, Type objectType, object existingValue, Namespace name)
 {
     throw new NotImplementedException();
 }
示例#8
0
 public override Task WriteAsync(RedisConvert convert, Type objectType, object value, Namespace name, TimeSpan?TTL = null)
 {
     throw new NotImplementedException();
 }
示例#9
0
 public abstract Task WriteAsync(RedisConvert convert, Type objectType, object value, Namespace name, TimeSpan?TTL = null);
示例#10
0
 public abstract Task <object> ReadAsync(RedisConvert convert, Type objectType, object existingValue, Namespace name);