/// <summary> /// Creates the dictionary. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="redisDb">The redis database.</param> /// <param name="redisKey">The redis key.</param> /// <param name="valueSerializer">The value serializer.</param> /// <returns></returns> public IRedisDictionary <TKey, TValue> CreateDictionary <TKey, TValue>( IDatabase redisDb, RedisKey redisKey, IRedisSerializer valueSerializer = null) { return(new RedisDictionary <TKey, TValue>(redisDb, redisKey, valueSerializer)); }
public RedisSubscriber(IConnectionMultiplexerFactory connectionFactory, IRedisSerializer serializer, FilterAttachedMessageHandlerFactory messageHandlerFactory, FilterAttachedAsyncMessageHandlerFactory asyncMessageHandlerFactory) { this.connectionFactory = connectionFactory; this.serializer = serializer; this.messageHandlerFactory = messageHandlerFactory; this.asyncMessageHandlerFactory = asyncMessageHandlerFactory; }
/// <summary> /// Creates the stack. /// </summary> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="redisDb">The redis database.</param> /// <param name="redisKey">The redis key.</param> /// <param name="valueSerializer">The value serializer.</param> /// <returns></returns> public IRedisStack <TValue> CreateStack <TValue>( IDatabase redisDb, RedisKey redisKey, IRedisSerializer valueSerializer = null) { return(new RedisStack <TValue>(redisDb, redisKey, valueSerializer)); }
/// <summary> /// Initializes the RedisSessionStateStoreProvider, reading in settings from the web.config /// SessionState element /// </summary> /// <param name="name">The name of the SessionStateStoreProvider, defaults to /// RedisSessionStateStore</param> /// <param name="config">A collection of metadata about the provider, can be empty</param> public override void Initialize(string name, NameValueCollection config) { // config will contain attribute values from the provider tag in the web.config if (string.IsNullOrEmpty(config["description"])) { config.Remove("description"); config.Add("description", "Redis Session State Store Provider"); } if (string.IsNullOrEmpty(name)) { name = "RedisSessionProvider"; } // the base Provider class is a .NET pattern that allows for configurable persistence layers or "providers". // might as well initialize the base with the description attribute and application name base.Initialize(name, config); // Get <sessionState> configuration element. System.Configuration.Configuration webCfg = WebConfigurationManager.OpenWebConfiguration( HostingEnvironment.ApplicationVirtualPath); SessionStateSection sessCfg = (SessionStateSection)webCfg.GetSection("system.web/sessionState"); this.SessionTimeout = sessCfg.Timeout; this.cereal = RedisSerializationConfig.SessionDataSerializer; }
public static void NullSerializer(IRedisSerializer redisSerializer, IRedisSerializer serializer) { if (redisSerializer == null && serializer == null) { throw new ArgumentNullException(nameof(IRedisSerializer), "请注入或者传入Redis序列化组件,并实现IRedisSerializer接口"); } }
/// <summary> /// Initializes the RedisSessionStateStoreProvider, reading in settings from the web.config /// SessionState element /// </summary> /// <param name="name">The name of the SessionStateStoreProvider, defaults to /// RedisSessionStateStore</param> /// <param name="config">A collection of metadata about the provider, can be empty</param> public override void Initialize(string name, NameValueCollection config) { // config will contain attribute values from the provider tag in the web.config if (string.IsNullOrEmpty(config["description"])) { config.Remove("description"); config.Add("description", "Redis Session State Store Provider"); } if (string.IsNullOrEmpty(name)) { name = "RedisSessionProvider"; } // the base Provider class is a .NET pattern that allows for configurable persistence layers or "providers". // might as well initialize the base with the description attribute and application name base.Initialize(name, config); // Get <sessionState> configuration element. var webCfg = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath); SessionStateSection sessCfg = (SessionStateSection)webCfg.GetSection("system.web/sessionState"); this.SessionTimeout = sessCfg.Timeout; this.cereal = RedisSerializationConfig.SessionDataSerializer; }
/// <summary> /// Instantiates a new instance of the RedisSessionStateItemCollection class with data from /// Redis /// </summary> /// <param name="redisHashData">An array of keys to values from the redis hash</param> /// <param name="redisConnName">The name of the connection from the redis connection wrapper</param> public RedisSessionStateItemCollection( HashEntry[] redisHashData, string redisConnName, byte constructorSignatureDifferentiator) { int byteDataTotal = 0; int concLevel = RedisSessionConfig.SessionAccessConcurrencyLevel; if (concLevel < 1) { concLevel = 1; } int numItems = 0; if (redisHashData != null) { numItems = redisHashData.Length; } // To match ASP.NET behavior, dictionaries should match keys case insensitively this.Items = new ConcurrentDictionary <string, object>(concLevel, numItems, StringComparer.InvariantCultureIgnoreCase); this.SerializedRawData = new ConcurrentDictionary <string, string>(concLevel, numItems, StringComparer.InvariantCultureIgnoreCase); if (redisHashData != null) { foreach (var sessDataEntry in redisHashData) { string hashItemKey = sessDataEntry.Name.ToString(); string hashItemValue = sessDataEntry.Value.ToString(); if (this.SerializedRawData.TryAdd( hashItemKey, hashItemValue)) { this.Items.TryAdd( hashItemKey, new NotYetDeserializedPlaceholderValue()); } byteDataTotal += hashItemValue.Length; } } // To match ASP.NET behavior, dictionaries should match keys case insensitively this.ChangedKeysDict = new ConcurrentDictionary <string, ActionAndValue>(StringComparer.InvariantCultureIgnoreCase); if (byteDataTotal != 0 && !string.IsNullOrEmpty(redisConnName) && RedisConnectionConfig.LogRedisSessionSize != null) { RedisConnectionConfig.LogRedisSessionSize(redisConnName, byteDataTotal); } this.cereal = RedisSerializationConfig.SessionDataSerializer; if (byteDataTotal > RedisConnectionConfig.MaxSessionByteSize) { RedisConnectionConfig.RedisSessionSizeExceededHandler(this, byteDataTotal); } }
public RedisDatabase(int dbIndex, RedisHelper redisHelper) { _dbIndex = dbIndex < 0 ? 0 : dbIndex; _redisHelper = redisHelper; _redis = redisHelper._redis; _redisSerializer = redisHelper._redisSerializer; _db = _redis.GetDatabase(_dbIndex); }
public ConfigProvider(RedisHelper redisHelper, IConfigCollection configs, IRedisSerializer redisSerializer, IConfigStorageProvider storageProvider, IConfiguration cfg) { _redisHelper = redisHelper; _configs = configs; _redisSerializer = redisSerializer; _storageProvider = storageProvider; _cfg = cfg; }
protected RedisClient(RedisConnectionStringBuilder connectionString, IRedisSerializer serializer) { ConnectionString = connectionString.ToString(); _connectionStringBuilder = connectionString; Serializer = serializer ?? DefaultSerializer; var socket = new Socket(_connectionStringBuilder.EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _channel = PrepareRedisChannel(socket); }
/// <summary> /// Initializes a new instance of the <see cref="RedisDictionary{TKey, TValue}" /> class. /// </summary> /// <param name="redisDb">The redis database.</param> /// <param name="redisKey">The redis key.</param> /// <param name="valueSerializer">The value serializer.</param> public RedisDictionary( IDatabase redisDb, RedisKey redisKey, IRedisSerializer valueSerializer = null) { RedisDb = redisDb; RedisKey = redisKey; ValueSerializer = valueSerializer ?? new JsonRedisSerializer(); KeySerializer = new JsonRedisSerializer(); }
public StackExchangeRedisProvider(IConfiguration configuration, IRedisSerializer redisSerializer) : base(configuration, redisSerializer) { // 读取配置文件中的Redis字符串信息 if (connMultiplexer == null) { var config = new List <StackExchangeConnectionSettings>(); configuration.GetSection("StackExchangeConnectionSettings").Bind(config); connMultiplexer = config; } }
/// <summary> /// Initializes a new instance of the <see cref="RedisMetadataCache"/> class /// </summary> public RedisMetadataCache(IConnectionStringProvider connectionStringProvider, IRedisSerializer redisSerializer, string keySpace, IMetadataCacheTracer cacheTracer, TimeSpan?cacheKeyBumpTime = null) { Contract.Requires(connectionStringProvider != null); Contract.Requires(redisSerializer != null); Contract.Requires(!string.IsNullOrWhiteSpace(keySpace)); ConnectionStringProvider = connectionStringProvider; _redisSerializer = redisSerializer; Keyspace = keySpace; _cacheTracer = cacheTracer; CacheKeyBumpTime = cacheKeyBumpTime.GetValueOrDefault(DefaultCacheKeyBumpTime); }
public RedisSessionStateItemCollection( Dictionary <string, string> redisHashData, string redisConnName) { int byteDataTotal = 0; int concLevel = RedisSessionConfig.SessionAccessConcurrencyLevel; if (concLevel < 1) { concLevel = 1; } int numItems = 0; if (redisHashData != null) { numItems = redisHashData.Count; } this.Items = new ConcurrentDictionary <string, object>(concLevel, numItems); this.SerializedRawData = new ConcurrentDictionary <string, string>(concLevel, numItems); if (redisHashData != null) { foreach (var sessDataEntry in redisHashData) { string hashItemKey = sessDataEntry.Key; string hashItemValue = sessDataEntry.Value; if (this.SerializedRawData.TryAdd(hashItemKey, hashItemValue)) { this.Items.TryAdd(hashItemKey, new NotYetDeserializedPlaceholderValue()); } byteDataTotal += hashItemValue.Length; } } this.ChangedKeysDict = new ConcurrentDictionary <string, ActionAndValue>(); if (byteDataTotal != 0 && !string.IsNullOrEmpty(redisConnName) && RedisConnectionConfig.LogRedisSessionSize != null) { RedisConnectionConfig.LogRedisSessionSize(redisConnName, byteDataTotal); } this.cereal = RedisSerializationConfig.SessionDataSerializer; if (byteDataTotal > RedisConnectionConfig.MaxSessionByteSize) { RedisConnectionConfig.RedisSessionSizeExceededHandler(this, byteDataTotal); } }
public bool SetRemove <T>(string key, T value, string connectionName = null, IRedisSerializer serializer = null) { RedisThrow.NullSerializer(redisSerializer, serializer); return(ExecuteCommand(ConnectTypeEnum.Write, connectionName, (db) => { if (serializer != null) { return db.SetRemove(key, serializer.Serializer(value)); } return db.SetRemove(key, redisSerializer.Serializer(value)); })); }
public T ListRightPop <T>(string key, string connectionName = null, IRedisSerializer serializer = null) { RedisThrow.NullSerializer(redisSerializer, serializer); return(ExecuteCommand(ConnectTypeEnum.Read, connectionName, (db) => { var value = db.ListRightPop(key); if (serializer != null) { return serializer.Deserialize <T>(value); } return redisSerializer.Deserialize <T>(value); })); }
protected BaseRedisClient(int dbNum, string redisConfiguration, IRedisSerializer serializer) { _dbNumber = dbNum; _redisConnectionString = redisConfiguration ?? GetRedisConnectionString(); _serializer = serializer; if (string.IsNullOrWhiteSpace(_redisConnectionString)) { throw new ArgumentNullException(nameof(redisConfiguration)); } if (serializer == null) { throw new ArgumentNullException(nameof(serializer)); } Initial(); }
/// <summary> /// Initializes a new instance of the <see cref="RedisCache" /> class. /// </summary> /// <param name="connection">The connection.</param> /// <param name="cacheKeyLockAcquisition">The cache key lock acquisition.</param> /// <param name="container">The container.</param> /// <param name="serializer">The serializer.</param> public RedisCache( IRedisConnection connection, IRedisDLM cacheKeyLockAcquisition, IRedisCacheContainer container = null, IRedisSerializer serializer = null) { Guard.AgainstNull(() => connection); Guard.AgainstNull(() => cacheKeyLockAcquisition); Disposed = false; _connection = connection; _cacheKeyLockAcquisition = cacheKeyLockAcquisition; _redisDb = connection.GetDatabase(); _container = container ?? new StringRedisCacheContainer(_connection); _serialier = serializer ?? new JsonRedisSerializer(); }
public TResult HashGetOrInsert <TResult>(string hashId, string key, Func <TResult> fetcher, int seconds = 0, string connectionRead = null, string connectionWrite = null, bool isCache = true, IRedisSerializer serializer = null) { RedisThrow.NullSerializer(redisSerializer, serializer); if (!isCache) { return(fetcher.Invoke()); } if (!HashExists(hashId, key, connectionRead)) { var source = fetcher.Invoke(); if (source != null) { if (seconds > 0) { bool exists = KeyExists(hashId, connectionRead); HashSet(hashId, key, source, connectionWrite, serializer); if (!exists) { KeyExpire(hashId, seconds, connectionWrite); } } else { HashSet(hashId, key, source, connectionWrite, serializer); } } return(source); } else { return(HashGet <TResult>(hashId, key, connectionRead, serializer)); } }
/// <summary> /// Instantiates a new instance of the RedisSessionStateItemCollection class with data from /// Redis /// </summary> /// <param name="redisHashData">An array of keys to values from the redis hash</param> /// <param name="redisConnName">The name of the connection from the redis connection wrapper</param> public RedisSessionStateItemCollection(HashEntry[] redisHashData, string redisKey) { int concLevel = Math.Max(RedisSessionConfig.SessionAccessConcurrencyLevel, 1); int numItems = redisHashData != null ? redisHashData.Length : 0; Items = new ConcurrentDictionary <string, object>(concLevel, numItems); SerializedRawData = new ConcurrentDictionary <string, string>(concLevel, numItems); ChangedKeysDict = new ConcurrentDictionary <string, ActionAndValue>(); Serializer = RedisSerializationConfig.SessionDataSerializer; if (numItems > 0) { int byteDataTotal = 0; foreach (var sessDataEntry in redisHashData) { string hashItemKey = sessDataEntry.Name.ToString(); string hashItemValue = sessDataEntry.Value.ToString(); if (this.SerializedRawData.TryAdd(hashItemKey, hashItemValue)) { this.Items.TryAdd(hashItemKey, new NotYetDeserializedPlaceholderValue()); } byteDataTotal += hashItemValue.Length; } if (byteDataTotal > 0 && !string.IsNullOrEmpty(redisKey) && RedisConnectionConfig.LogRedisSessionSize != null) { RedisConnectionConfig.LogRedisSessionSize(RedisSessionProvider.Redis.RedisConnectionWrapper.GetConnectionId(redisKey), byteDataTotal); } if (byteDataTotal > RedisConnectionConfig.MaxSessionByteSize) { RedisConnectionConfig.RedisSessionSizeExceededHandler(this, byteDataTotal); } } }
public TResult StringGetOrInsert <T, TResult>(string key, Func <T, TResult> fetcher, T t, int seconds = 0, string connectionRead = null, string connectionWrite = null, bool isCache = true, IRedisSerializer serializer = null) { RedisThrow.NullSerializer(redisSerializer, serializer); if (!isCache) { return(fetcher.Invoke(t)); } if (!KeyExists(key, connectionRead)) { var source = fetcher.Invoke(t); if (source != null) { StringSet(key, source, seconds, connectionWrite, serializer); } return(source); } else { return(StringGet <TResult>(key, connectionRead, serializer)); } }
public async Task <Dictionary <T, double> > SortedRangeAsync <T>(string key, long start, long stop, int orderby = 0, string connectionName = null, IRedisSerializer serializer = null) { RedisThrow.NullSerializer(redisSerializer, serializer); return(await ExecuteCommand(ConnectTypeEnum.Read, connectionName, async (db) => { Order o = orderby == 1 ? Order.Descending : Order.Ascending; var resultEntry = await db.SortedSetRangeByRankWithScoresAsync(key, start, stop, order: o); if (serializer != null) { return resultEntry.ToDictionary(t => serializer.Deserialize <T>(t.Element), t => t.Score); } return resultEntry.ToDictionary(t => redisSerializer.Deserialize <T>(t.Element), t => t.Score); })); }
public async Task <IList <T> > SortedRangeByScoreAsync <T>(string key, double start, double stop, int orderby = 0, int skip = 0, int take = -1, string connectionName = null, IRedisSerializer serializer = null) { RedisThrow.NullSerializer(redisSerializer, serializer); return(await ExecuteCommand(ConnectTypeEnum.Read, connectionName, async (db) => { Order o = orderby == 1 ? Order.Descending : Order.Ascending; var resultEntry = await db.SortedSetRangeByScoreAsync(key, start, stop, order: o, skip: skip, take: take); if (serializer != null) { return serializer.Deserialize <T>(resultEntry); } return redisSerializer.Deserialize <T>(resultEntry); })); }
/// <summary> /// Initializes a new instance of the <see cref="RedisQueue{T}" /> class. /// </summary> /// <param name="redisDb">The redis database.</param> /// <param name="redisKey">The redis key.</param> /// <param name="serializer">The serializer.</param> public RedisQueue(IDatabase redisDb, RedisKey redisKey, IRedisSerializer serializer = null) { _redisList = new RedisList <T>(redisDb, redisKey, serializer); }
public MultiThreadedTests() { this.items = new RedisSessionStateItemCollection(); this.srsly = new RedisJsonSerializer(); }
public async Task <long> SortedRemoveAsync <T>(string key, IList <T> values, string connectionName = null, IRedisSerializer serializer = null) { RedisThrow.NullSerializer(redisSerializer, serializer); return(await ExecuteCommand(ConnectTypeEnum.Write, connectionName, async (db) => { List <RedisValue> listValues = new List <RedisValue>(); foreach (var val in values) { if (serializer != null) { listValues.Add(serializer.Serializer(val)); } else { listValues.Add(redisSerializer.Serializer(val)); } } return await db.SortedSetRemoveAsync(key, listValues.ToArray()); })); }
public void OnBeforeTestExecute() { this.items = new RedisSessionStateItemCollection(); this.srsly = new RedisJSONSerializer(); }
public void EngageWith(IRedisSerializer serializer) { _redisReceiver.EngageWith(serializer); }
public async Task <long?> SortedZrankAsync <T>(string key, T value, int orderby = 0, string connectionName = null, IRedisSerializer serializer = null) { RedisThrow.NullSerializer(redisSerializer, serializer); return(await ExecuteCommand(ConnectTypeEnum.Read, connectionName, async (db) => { Order o = orderby == 1 ? Order.Descending : Order.Ascending; if (serializer != null) { return await db.SortedSetRankAsync(key, serializer.Serializer(value), o); } return await db.SortedSetRankAsync(key, redisSerializer.Serializer(value), o); })); }
public async Task <long> SortedAddAsync <T>(string key, Dictionary <T, double> values, string connectionName = null, IRedisSerializer serializer = null) { RedisThrow.NullSerializer(redisSerializer, serializer); return(await ExecuteCommand(ConnectTypeEnum.Write, connectionName, async (db) => { List <SortedSetEntry> sortedEntry = new List <SortedSetEntry>(); foreach (var keyvalue in values) { if (serializer != null) { var entry = new SortedSetEntry(serializer.Serializer(keyvalue.Key), keyvalue.Value); sortedEntry.Add(entry); } else { var entry = new SortedSetEntry(redisSerializer.Serializer(keyvalue.Key), keyvalue.Value); sortedEntry.Add(entry); } } return await db.SortedSetAddAsync(key, sortedEntry.ToArray()); })); }
public void EngageWith(IRedisSerializer serializer) { _serializer = serializer; }
public async Task <bool> SortedRemoveAsync <T>(string key, T value, string connectionName = null, IRedisSerializer serializer = null) { RedisThrow.NullSerializer(redisSerializer, serializer); return(await ExecuteCommand(ConnectTypeEnum.Write, connectionName, async (db) => { if (value == null) { return false; } if (serializer != null) { return await db.SortedSetRemoveAsync(key, serializer.Serializer(value)); } return await db.SortedSetRemoveAsync(key, redisSerializer.Serializer(value)); })); }
internal Bulk(byte[] value, IRedisSerializer serializer) : base(RedisBoost.ResponseType.Bulk, serializer) { Value = value; }
public void EngageWith(IRedisSerializer serializer) { _redisReceiver.EngageWith(serializer); }
public RedisSessionStateItemCollection(Dictionary<string, byte[]> redisHashData, string redisConnName) { int byteDataTotal = 0; int concLevel = RedisSessionConfig.SessionAccessConcurrencyLevel; if (concLevel < 1) { concLevel = 1; } int numItems = 0; if (redisHashData != null) { numItems = redisHashData.Count; } // To match ASP.NET behavior, dictionaries should match keys case insensitively this.Items = new ConcurrentDictionary<string, object>(concLevel, numItems, StringComparer.InvariantCultureIgnoreCase); this.SerializedRawData = new ConcurrentDictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); if (redisHashData != null) { foreach (var sessDataEntry in redisHashData) { if (this.SerializedRawData.TryAdd( sessDataEntry.Key, Encoding.UTF8.GetString(sessDataEntry.Value))) { this.Items.TryAdd( sessDataEntry.Key, new NotYetDeserializedPlaceholderValue()); } byteDataTotal += sessDataEntry.Value.Length; } } // To match ASP.NET behavior, dictionaries should match keys case insensitively this.ChangedKeysDict = new ConcurrentDictionary<string, ActionAndValue>(StringComparer.InvariantCultureIgnoreCase); if (byteDataTotal != 0 && !string.IsNullOrEmpty(redisConnName) && RedisConnectionConfig.LogRedisSessionSize != null) { RedisConnectionConfig.LogRedisSessionSize(redisConnName, byteDataTotal); } this.cereal = RedisSerializationConfig.SessionDataSerializer; if (byteDataTotal > RedisConnectionConfig.MaxSessionByteSize) { RedisConnectionConfig.RedisSessionSizeExceededHandler(this, byteDataTotal); } }
public void EngageWith(IRedisSerializer serializer) { _serializer = serializer; }
/// <summary> /// Instantiates a new instance of the RedisSessionStateItemCollection class with data from /// Redis /// </summary> /// <param name="redisHashData">An array of keys to values from the redis hash</param> /// <param name="redisConnName">The name of the connection from the redis connection wrapper</param> public RedisSessionStateItemCollection( HashEntry[] redisHashData, string redisConnName, byte constructorSignatureDifferentiator) { int byteDataTotal = 0; int concLevel = RedisSessionConfig.SessionAccessConcurrencyLevel; if (concLevel < 1) { concLevel = 1; } int numItems = 0; if (redisHashData != null) { numItems = redisHashData.Length; } this.Items = new ConcurrentDictionary<string, object>(concLevel, numItems); this.SerializedRawData = new ConcurrentDictionary<string, string>(concLevel, numItems); if (redisHashData != null) { foreach (var sessDataEntry in redisHashData) { string hashItemKey = sessDataEntry.Name.ToString(); string hashItemValue = sessDataEntry.Value.ToString(); if (this.SerializedRawData.TryAdd( hashItemKey, hashItemValue)) { this.Items.TryAdd( hashItemKey, new NotYetDeserializedPlaceholderValue()); } byteDataTotal += hashItemValue.Length; } } this.ChangedKeysDict = new ConcurrentDictionary<string, ActionAndValue>(); if (byteDataTotal != 0 && !string.IsNullOrEmpty(redisConnName) && RedisConnectionConfig.LogRedisSessionSize != null) { RedisConnectionConfig.LogRedisSessionSize(redisConnName, byteDataTotal); } this.cereal = RedisSerializationConfig.SessionDataSerializer; if (byteDataTotal > RedisConnectionConfig.MaxSessionByteSize) { RedisConnectionConfig.RedisSessionSizeExceededHandler(this, byteDataTotal); } }