コード例 #1
0
ファイル: RedisSubject.cs プロジェクト: qyen/CloudStructures
        public static RemotableNotification <T> ReadFrom(Stream stream, IRedisValueConverter converter)
        {
            using (var br = new BinaryReader(stream, Encoding.UTF8, leaveOpen: false))
            {
                var value = new RemotableNotification <T>();
                value.Kind = (NotificationKind)br.ReadInt32();
                switch (value.Kind)
                {
                case NotificationKind.OnCompleted:
                    break;

                case NotificationKind.OnError:
                    value.ErrorMessage = br.ReadString();
                    break;

                case NotificationKind.OnNext:
                    using (var restMemory = new MemoryStream())
                    {
                        stream.CopyTo(restMemory);
                        value.Value = converter.Deserialize <T>(restMemory.ToArray());
                    }
                    break;

                default:
                    throw new InvalidOperationException("Invalid Kind");
                }

                return(value);
            }
        }
コード例 #2
0
ファイル: RedisSubject.cs プロジェクト: qyen/CloudStructures
 public RedisSubject(RedisSettings settings, string key, PubSubKeyType keyType = PubSubKeyType.Normal)
 {
     this.settings       = settings;
     this.Db             = settings.Db;
     this.valueConverter = settings.ValueConverter;
     this.Key            = key;
     this.keyType        = keyType;
 }
コード例 #3
0
 public RedisSettings(ConfigurationOptions configuration, int db = 0, IRedisValueConverter converter = null, Func <ICommandTracer> tracerFactory = null, System.IO.TextWriter connectionMultiplexerLog = null)
 {
     this.configuration            = configuration;
     this.Db                       = db;
     this.ValueConverter           = converter ?? new JsonRedisValueConverter();
     this.CommandTracerFactory     = tracerFactory;
     this.connectionMultiplexerLog = connectionMultiplexerLog;
 }
コード例 #4
0
        private static void RegisterConverter(Dictionary <Type, IRedisValueConverter> converters, Type type, IRedisValueConverter converter)
        {
            converters[type] = converter;

            // auto register for Nullable type
            if (type.GetTypeInfo().IsValueType)
            {
                var nullableType      = typeof(Nullable <>).MakeGenericType(type);
                var converterType     = typeof(NullableTypeConverter <>).MakeGenericType(type);
                var nullableConverter = Activator.CreateInstance(converterType, converter) as IRedisValueConverter;
                converters[nullableType] = nullableConverter;
            }
        }
コード例 #5
0
        /// <summary>
        /// Register a customer IRedisValueConverter used to convert <paramref name="type"/> from or to RedisValue
        /// Note that registering custom IRedisValueConverter is intended to occur when startup and is not thread safe
        /// </summary>
        /// <param name="type">type to convert</typeparam>
        /// <param name="converter">cumtomer IRedisValueConverter to registyer</param>
        /// <exception cref="ArgumentNullException">throw if <paramref name="type"/> or <paramref name="converter"/> is null</exception>
        public static void RegisterConverter(Type type, IRedisValueConverter converter)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            RegisterConverter(_customConverters, type, converter);
        }
コード例 #6
0
 public RedisSettings(string host, int port = 6379, int ioTimeout = -1, string password = null, int maxUnsent = 2147483647, bool allowAdmin = false, int syncTimeout = 10000, int db = 0, IRedisValueConverter converter = null, Func<ICommandTracer> tracerFactory = null)
 {
     this.Host = host;
     this.Port = port;
     this.IoTimeout = ioTimeout;
     this.Password = password;
     this.MaxUnsent = maxUnsent;
     this.AllowAdmin = allowAdmin;
     this.SyncTimeout = syncTimeout;
     this.Db = db;
     this.ValueConverter = converter ?? new JsonRedisValueConverter();
     this.CommandTracerFactory = tracerFactory;
 }
コード例 #7
0
ファイル: RedisSubject.cs プロジェクト: qyen/CloudStructures
        public void WriteTo(Stream stream, IRedisValueConverter converter)
        {
            using (var bw = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: false))
            {
                bw.Write((int)Kind);
                switch (Kind)
                {
                case NotificationKind.OnCompleted:
                    break;

                case NotificationKind.OnError:
                    bw.Write(ErrorMessage);
                    break;

                case NotificationKind.OnNext:
                    bw.Write(converter.Serialize(Value));
                    break;

                default:
                    throw new InvalidOperationException("Invalid Kind");
                }
            }
        }
コード例 #8
0
 /// <summary>
 /// Register a customer IRedisValueConverter used to convert <typeparamref name="T"/> from or to RedisValue
 /// Note that registering custom IRedisValueConverter is intended to occur when startup and is not thread safe
 /// </summary>
 /// <typeparam name="T">type to convert</typeparam>
 /// <param name="converter">cumtomer IRedisValueConverter<T> to registyer</param>
 /// <exception cref="ArgumentNullException">throw if convert is null</exception>
 public static void RegisterConverter <T>(IRedisValueConverter <T> converter)
 {
     RegisterConverter(typeof(T), converter);
 }
コード例 #9
0
        internal static async Task HashEntrySetAsync <TValue>(this IDatabase cache, string key, CacheEntry <string, TValue> entry, IRedisValueConverter valueConverter)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            var hashEntries = ConvertHashEntry(entry, valueConverter);

            await cache.HashSetAsync(key, hashEntries, CommandFlags.PreferMaster).ConfigureAwait(false);
        }
コード例 #10
0
 public RedisSettings(string connectionString, int db = 0, IRedisValueConverter converter = null, Func <ICommandTracer> tracerFactory = null, System.IO.TextWriter connectionMultiplexerLog = null)
     : this(ConfigurationOptions.Parse(connectionString), db, converter, tracerFactory, connectionMultiplexerLog)
 {
 }
コード例 #11
0
 public NullableTypeConverter(IRedisValueConverter <T> valueTypeConverter)
 {
     _valueTypeConverter = valueTypeConverter;
 }
コード例 #12
0
        internal static void HashEntrySet <TValue>(this IDatabase cache, string key, CacheEntry <string, TValue> entry, IRedisValueConverter valueConverter)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            var hashEntries = ConvertHashEntry(entry, valueConverter);

            cache.HashSet(key, hashEntries, CommandFlags.PreferMaster);
        }
コード例 #13
0
        private static HashEntry[] ConvertHashEntry <TValue>(CacheEntry <string, TValue> entry, IRedisValueConverter valueConverter)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }

            if (valueConverter == null)
            {
                throw new ArgumentNullException(nameof(valueConverter));
            }

            var hashEntries = new HashEntry[]
            {
                new HashEntry(RedisCacheConstants.HashField_Key, entry.Key),
                new HashEntry(RedisCacheConstants.HashField_Region, entry.Region ?? string.Empty),
                new HashEntry(RedisCacheConstants.HashField_Value, valueConverter.ToRedisValue(entry.Value)),
                new HashEntry(RedisCacheConstants.HashField_Options, valueConverter.ToRedisValue(entry.Options))
            };

            return(hashEntries);
        }
コード例 #14
0
        internal static async Task <CacheEntry <string, TValue> > HashEntryGetAsync <TValue>(this IDatabase cache, string key, IRedisValueConverter valueConverter)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            var results = await cache.HashGetAsync(key, EntryHashFields, CommandFlags.PreferSlave).ConfigureAwait(false);

            return(ConvertCacheEntry <TValue>(results, valueConverter));
        }
コード例 #15
0
        private static CacheEntry <string, TValue> ConvertCacheEntry <TValue>(RedisValue[] redisValues, IRedisValueConverter valueConverter)
        {
            if (redisValues == null || redisValues.Length < 4)
            {
                return(null);
            }

            if (valueConverter == null)
            {
                throw new ArgumentNullException(nameof(valueConverter));
            }

            var keyItem     = redisValues[0];
            var regionItem  = redisValues[1];
            var valueItem   = redisValues[2];
            var optionsItem = redisValues[3];

            if (!valueItem.HasValue || valueItem.IsNull)  /* partially removed? */
            {
                return(null);
            }

            var cacheEntry = new CacheEntry <string, TValue>(keyItem, regionItem,
                                                             valueConverter.FromRedisValue <TValue>(valueItem),
                                                             valueConverter.FromRedisValue <CacheEntryOptions>(optionsItem));

            return(cacheEntry);
        }
コード例 #16
0
 public static T Deserialize <T>(this IRedisValueConverter converter, byte[] value)
 {
     return((T)converter.Deserialize(typeof(T), value));
 }
コード例 #17
0
 public RedisSettings(string host, int port = 6379, int ioTimeout = -1, string password = null, int maxUnsent = 2147483647, bool allowAdmin = false, int syncTimeout = 10000, int db = 0, IRedisValueConverter converter = null, Func <ICommandTracer> tracerFactory = null)
 {
     this.Host                 = host;
     this.Port                 = port;
     this.IoTimeout            = ioTimeout;
     this.Password             = password;
     this.MaxUnsent            = maxUnsent;
     this.AllowAdmin           = allowAdmin;
     this.SyncTimeout          = syncTimeout;
     this.Db                   = db;
     this.ValueConverter       = converter ?? new JsonRedisValueConverter();
     this.CommandTracerFactory = tracerFactory;
 }
コード例 #18
0
 public static T Deserialize <T>(this IRedisValueConverter converter, RedisValue value, out long valueSize)
 {
     return((T)converter.Deserialize(typeof(T), value, out valueSize));
 }