/// <summary> /// Creates a new instance of <see cref="RedisCacheLayer"/> with the given <paramref name="connection"/> and <paramref name="databaseIndex"/>. /// </summary> /// <param name="connection">The primary connection to Redis where the cache will be stored.</param> /// <param name="serializer"> /// Allows you to specify which encoding should be used by providing your own serializer /// If one is not provided, ProtobufCacheSerializer will be used /// </param> /// <param name="databaseIndex"> /// The database index to use for Redis. /// If not specified, uses the default database as configured on the <paramref name="connection"/>. /// </param> public RedisCacheLayer(IConnectionMultiplexer connection, ICacheSerializer serializer, int databaseIndex = -1) { Connection = connection; Database = connection.GetDatabase(databaseIndex); DatabaseIndex = databaseIndex; Serializer = serializer; }
public void ReLoadRedis() { try { RedisConfig = RedisSettingFactory.Load(); ICacheSerializer serializer = RedisConfig.Serializer == StorageMode.Protobuf ? (ICacheSerializer) new ProtobufCacheSerializer() : new JsonCacheSerializer(Encoding.UTF8); RedisConnectionPool.Initialize(new RedisPoolSetting() { Host = RedisConfig.Host, ReadOnlyHost = RedisConfig.Host, MaxWritePoolSize = 2, MaxReadPoolSize = 2, DbIndex = RedisConfig.Db }, serializer); var _redisSection = ConfigManager.Configger.GetFirstOrAddConfig <RedisSection>(); if (_redisSection == null) { } } catch (Exception ex) { TraceLog.WriteError("ReLoadRedis error:{0}", ex); } }
public SQLiteCacheHandle( ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, ICacheSerializer serializer, SQLiteCacheHandleAdditionalConfiguration additionalConfiguration) : base(managerConfiguration, configuration) { this.serializer = serializer; this.additionalConfiguration = additionalConfiguration ?? new SQLiteCacheHandleAdditionalConfiguration(); Logger = loggerFactory.CreateLogger(this); this.cacheName = configuration.Name; this.conn = CreateConnection(this.additionalConfiguration.DatabaseFilePath); if (additionalConfiguration != null) { additionalConfiguration.BeginTransactionMethod = () => this.conn.BeginTransaction(/* TODO: Support arguments/overloads */); } this.SetInitialItemCount(); RemoveExpiredItems(); }
/// <summary> /// Start /// </summary> /// <param name="setting"></param> /// <param name="serializer"></param> public static void Start(CacheSetting setting, ICacheSerializer serializer) { _serializer = serializer; _entityQueueTimer = new Timer(OnEntitySyncQueue, null, 60, 500); _entityQueueWacher = new Timer(CheckEntityQueue, null, 60, 60000); _queueWatchTimers = new Timer[DataSyncQueueNum]; for (int i = 0; i < DataSyncQueueNum; i++) { _queueWatchTimers[i] = new Timer(OnCheckRedisSyncQueue, i, 100, 100); } _threadPools = new SmartThreadPool(180 * 1000, 100, 5); _threadPools.Start(); _enableWriteToDb = setting.EnableWriteToDb; if (_enableWriteToDb) { int sqlSyncInterval = ConfigUtils.GetSetting("Game.Cache.UpdateDbInterval", 300 * 1000); _sqlWaitTimers = new Timer[SqlWaitSyncQueueNum]; for (int i = 0; i < SqlWaitSyncQueueNum; i++) { _sqlWaitTimers[i] = new Timer(OnCheckSqlWaitSyncQueue, i, 100, sqlSyncInterval); } SqlStatementManager.Start(); } }
public PersistenceService( ICacheSerializer cacheSerializer, ISaveSerializer saveSerializer, IParserService parserService) { this.cacheSerializer = cacheSerializer; this.saveSerializer = saveSerializer; this.parserService = parserService; }
public RealmDriver(string path, ICacheSerializer serializer) { // limit the types of objects we will store: Config = new RealmConfiguration(path); Config.ObjectClasses = new[] { typeof(RealmCache) }; Serializer = serializer; Log.Debug("Realm Cache Driver Initialized at '{Path}'", path); }
/// <summary> /// 初始化一个<see cref="DefaultRedisCacheProvider"/>类型的实例 /// </summary> /// <param name="dbProvider">Redis 数据库提供程序</param> /// <param name="serializer">缓存序列化器</param> public DefaultRedisCacheProvider(IRedisDatabaseProvider dbProvider, ICacheSerializer serializer) { dbProvider.CheckNotNull(nameof(dbProvider)); serializer.CheckNotNull(nameof(serializer)); _dbProvider = dbProvider; _serializer = serializer; _database = _dbProvider.GetDatabase(); Client = new RedisClient(_dbProvider); }
public ProductCacheRepository( ICacheSerializer cacheSerializer, IConfiguration configuration, IProductRepository concreteRepository, IDistributedCache distributedCache) { _concreteRepository = concreteRepository; _distributedCache = distributedCache; _cacheConfiguration = configuration.GetSection("Cache:Services:IProductRepository"); }
public InMemoryJsonHandle( ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ICacheSerializer serializer, ILoggerFactory loggerFactory) : base(managerConfiguration, configuration) { _cache = new ConcurrentDictionary <string, Tuple <Type, byte[]> >(); _serializer = serializer; Logger = loggerFactory.CreateLogger(this); }
//For test purposes public CachedFileSystemParser(IFileSystemParser fileSystemParser, ICacheSerializer cacheSerializer, IFileSystemListener fileSystemListener, IRegistryService registryService, IAsyncFileSystemParser asyncFileSystemParser, bool appRunOnStartup, int updatesCountToWrite) : this(fileSystemParser, cacheSerializer, fileSystemListener, registryService, asyncFileSystemParser, appRunOnStartup) { _updatesCountToWrite = updatesCountToWrite; }
/// <summary> /// Start /// </summary> /// <param name="setting"></param> /// <param name="serializer"></param> public static void Start(CacheSetting setting, ICacheSerializer serializer) { _serializer = serializer; //_threadPools = new SmartThreadPool(180 * 1000, 100, 5); _entityQueueTimer = new Timer(OnEntitySyncQueue, null, 60, 100); MessageQueueSection section = GetSection(); InitRedisQueue(section); InitSqlQueue(section); //_threadPools.Start(); }
public CachingInterceptor( IKeyGenerator keyGenerator, IDistributedCache cache, ICacheSerializer serializer, ILogger <CachingInterceptor> logger) { _keyGenerator = keyGenerator; _cache = cache; _serializer = serializer; _logger = logger; }
public DistributedCache(IOptions <CacheOptions> options) { _serializer = new JsonCacheSerializer(); _cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions { CompactionPercentage = 0.05, ExpirationScanFrequency = TimeSpan.FromMinutes(1.0), SizeLimit = null, Clock = new LocalServerTimestampService() })); _options = options; }
private static void InitSerializer() { string type = ConfigUtils.GetSetting("Cache.Serializer", "Protobuf"); if (string.Equals(type, "json", StringComparison.OrdinalIgnoreCase)) { serializer = new JsonCacheSerializer(Encoding.UTF8); } else { serializer = new ProtobufCacheSerializer(); } }
static RedisConnectionPool() { _serializer = new ProtobufCacheSerializer(); _defaultSetting = new RedisPoolSetting { Host = ConfigUtils.GetSetting("Redis.Host", "localhost"), MaxWritePoolSize = ConfigUtils.GetSetting("Redis.MaxWritePoolSize", 100), MaxReadPoolSize = ConfigUtils.GetSetting("Redis.MaxReadPoolSize", 100), ConnectTimeout = ConfigUtils.GetSetting("Redis.ConnectTimeout", 0), PoolTimeOut = ConfigUtils.GetSetting("Redis.PoolTimeOut", 0), DbIndex = ConfigUtils.GetSetting("Redis.Db", 0) }; _defaultSetting.ReadOnlyHost = ConfigUtils.GetSetting("Redis.ReadHost", _defaultSetting.Host); }
/// <summary> /// 初始化一个<see cref="DefaultRedisCacheProvider"/>类型的实例 /// </summary> /// <param name="dbProvider">Redis数据库提供程序</param> /// <param name="serializer">缓存序列化器</param> /// <param name="options">Redis选项</param> /// <param name="log">日志</param> public DefaultRedisCacheProvider(IRedisDatabaseProvider dbProvider, ICacheSerializer serializer, IOptionsMonitor <RedisOptions> options, ILog log = null) { Check.NotNull(dbProvider, nameof(dbProvider)); Check.NotNull(serializer, nameof(serializer)); this._dbProvider = dbProvider; this._serializer = serializer; this._options = options.CurrentValue; this._log = log ?? NullLog.Instance; this._cache = _dbProvider.GetDatabase(); this._servers = _dbProvider.GetServerList(); this.CacheStatsInfo = new CacheStatsInfo(); }
public DistributedCache( ICacheSerializer serializer, ICacheDeserializer deserializer, Func <DateTimeOffset> timestamps) { _serializer = serializer; _deserializer = deserializer; _cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions { CompactionPercentage = 0.05, ExpirationScanFrequency = TimeSpan.FromMinutes(1.0), SizeLimit = null, Clock = new DelegatedSystemClock(timestamps) })); }
public CachedFileSystemParser(IFileSystemParser fileSystemParser, ICacheSerializer cacheSerializer, IFileSystemListener fileSystemListener, IRegistryService registryService, IAsyncFileSystemParser asyncFileSystemParser, bool appRunOnStartup) { _cacheSerializer = cacheSerializer; _fileSystemListener = fileSystemListener; _registryService = registryService; _fileSystemParser = fileSystemParser; _asyncFileSystemParser = asyncFileSystemParser; _fileSystemFilter = new FileSystemFilter(); Initialize(appRunOnStartup); }
/// <summary> /// 初始化一个<see cref="DefaultRedisCacheProvider"/>类型的实例 /// </summary> /// <param name="name">名称</param> /// <param name="dbProviders">Redis数据库提供程序列表</param> /// <param name="serializer">缓存序列化器</param> /// <param name="options">Redis选项</param> /// <param name="log">日志</param> public DefaultRedisCacheProvider(string name, IEnumerable <IRedisDatabaseProvider> dbProviders, ICacheSerializer serializer, IOptionsMonitor <RedisOptions> options, ILog log = null) { Check.NotNullOrEmpty(dbProviders, nameof(dbProviders)); Check.NotNull(serializer, nameof(serializer)); this._dbProvider = dbProviders.FirstOrDefault(x => x.DbProviderName.Equals(name)); this._serializer = serializer; this._options = options.CurrentValue; this._log = log ?? NullLog.Instance; this._cache = _dbProvider.GetDatabase(); this._servers = _dbProvider.GetServerList(); this.CacheStatsInfo = new CacheStatsInfo(); this.Name = name; }
public MemcachedCacheHandle( ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, ICacheSerializer serializer, MemcachedClientConfiguration clientConfiguration) : this(configuration, managerConfiguration, loggerFactory) { NotNull(clientConfiguration, nameof(clientConfiguration)); if (clientConfiguration.Transcoder.GetType() == typeof(DefaultTranscoder)) { clientConfiguration.Transcoder = new CacheManagerTanscoder <TCacheValue>(serializer); } this.Cache = new MemcachedClient(clientConfiguration); }
public MemcachedCacheHandle( ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, ICacheSerializer serializer, MemcachedClientConfiguration clientConfiguration) : this(configuration, managerConfiguration, loggerFactory) { NotNull(clientConfiguration, nameof(clientConfiguration)); if (clientConfiguration.Transcoder.GetType() == typeof(DefaultTranscoder)) { clientConfiguration.Transcoder = new CacheManagerTanscoder <TCacheValue>(serializer); // default is 10, that might be too long as it can take up to 10sec to recover during retries clientConfiguration.SocketPool.DeadTimeout = TimeSpan.FromSeconds(2); } Cache = new MemcachedClient(clientConfiguration); }
public MemcachedCacheHandle( ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, ICacheSerializer serializer) : this(configuration, managerConfiguration, loggerFactory) { try { NotNull(configuration, nameof(configuration)); var sectionName = GetEnyimSectionName(configuration.Key); var section = GetSection(sectionName); Cache = new MemcachedClient(section); } catch (ConfigurationErrorsException ex) { throw new InvalidOperationException("Failed to initialize " + GetType().Name + ". " + ex.BareMessage, ex); } }
public CacheManagerConfiguration( CacheUpdateMode mode = CacheUpdateMode.None, int maxRetries = int.MaxValue, int retryTimeout = 10, Type backPlateType = null, string backPlateName = null, ICacheSerializer serializer = null) : this() { this.CacheUpdateMode = mode; this.MaxRetries = maxRetries; this.RetryTimeout = retryTimeout; this.BackPlateType = backPlateType; this.BackPlateName = backPlateName; #if !PORTABLE && !DOTNET5_2 // default to binary serialization if available this.CacheSerializer = serializer ?? new BinaryCacheSerializer(); #else this.CacheSerializer = serializer; #endif }
public CacheManagerConfiguration( CacheUpdateMode mode = CacheUpdateMode.None, int maxRetries = int.MaxValue, int retryTimeout = 10, Type backPlateType = null, string backPlateConfigurationKey = null, ICacheSerializer serializer = null) : this() { this.CacheUpdateMode = mode; this.MaxRetries = maxRetries; this.RetryTimeout = retryTimeout; this.BackPlateType = backPlateType; this.BackPlateConfigurationKey = backPlateConfigurationKey; #if !PORTABLE && !DOTNET5_2 // default to binary serialization if available this.CacheSerializer = serializer ?? new BinaryCacheSerializer(); #else this.CacheSerializer = serializer; #endif }
/// <summary> /// init /// </summary> /// <param name="setting">pool setting</param> /// <param name="serializer"></param> public static void Initialize(RedisPoolSetting setting, ICacheSerializer serializer) { _setting = setting; _serializer = serializer; //string[] readWriteHosts = setting.Host.Split(','); //string[] readOnlyHosts = setting.ReadOnlyHost.Split(','); //var redisConfig = new RedisClientManagerConfig //{ // MaxWritePoolSize = setting.MaxWritePoolSize, // MaxReadPoolSize = setting.MaxReadPoolSize, // DefaultDb = setting.DbIndex, // AutoStart = false //}; //_pooledRedis = new PooledRedisClientManager(readWriteHosts, readOnlyHosts, redisConfig); //if (setting.ConnectTimeout > 0) //{ // _pooledRedis.ConnectTimeout = setting.ConnectTimeout; //} //if (setting.PoolTimeOut > 0) //{ // _pooledRedis.PoolTimeout = setting.PoolTimeOut; //} //_pooledRedis.Start(); }
public MemcachedCacheHandle( ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, ICacheSerializer serializer) : this(configuration, managerConfiguration, loggerFactory) { try { NotNull(configuration, nameof(configuration)); var sectionName = GetEnyimSectionName(configuration.Key); var section = GetSection(sectionName); this.Cache = new MemcachedClient( section.CreatePool(), section.CreateKeyTransformer(), section.CreateTranscoder() ?? new CacheManagerTanscoder <TCacheValue>(serializer), section.CreatePerformanceMonitor()); } catch (ConfigurationErrorsException ex) { throw new InvalidOperationException("Failed to initialize " + this.GetType().Name + ". " + ex.BareMessage, ex); } }
/// <summary> /// init /// </summary> /// <param name="setting">pool setting</param> /// <param name="serializer"></param> public static void Initialize(RedisPoolSetting setting, ICacheSerializer serializer) { _serializer = serializer; string[] readWriteHosts = setting.Host.Split(','); string[] readOnlyHosts = setting.ReadOnlyHost.Split(','); var redisConfig = new RedisClientManagerConfig { MaxWritePoolSize = setting.MaxWritePoolSize, MaxReadPoolSize = setting.MaxReadPoolSize, DefaultDb = setting.DbIndex, AutoStart = false }; _pooledRedis = new PooledRedisClientManager(readWriteHosts, readOnlyHosts, redisConfig); if (setting.ConnectTimeout > 0) { _pooledRedis.ConnectTimeout = setting.ConnectTimeout; } if (setting.PoolTimeOut > 0) { _pooledRedis.PoolTimeout = setting.PoolTimeOut; } _pooledRedis.Start(); }
/// <summary> /// Initialize entity cache. /// </summary> public static void InitializeCache(ICacheSerializer serializer) { CacheFactory.Initialize(new CacheSetting(), serializer); }
/// <summary> /// Initialize cache. /// </summary> /// <param name="dbTransponder">db trans object</param> /// <param name="redisTransponder">redis trans object</param> /// <param name="setting">setting.</param> /// <param name="serializer"></param> public static void Initialize(ITransponder dbTransponder, ITransponder redisTransponder, CacheSetting setting, ICacheSerializer serializer) { _readonlyPools = new CachePool(dbTransponder, redisTransponder, true, serializer); _writePools = new CachePool(dbTransponder, redisTransponder, false, serializer) { Setting = setting }; EntitySchemaSet.InitSchema(typeof(EntityHistory)); DataSyncQueueManager.Start(setting, serializer); ProfileManager.Start(); InitListener("__CachePoolListener", setting.ExpiredInterval, "__CachePoolUpdateListener", setting.UpdateInterval); if (setting.AutoRunEvent) { StartListener(); } }
/// <summary> /// Initialize cache. /// </summary> public static void Initialize(CacheSetting setting, ICacheSerializer serializer) { Initialize(new DbTransponder(), new RedisTransponder(), setting, serializer); }
/// <summary> /// 通过Redis键获取实体对象 /// </summary> /// <param name="key"></param> /// <param name="isRemove"></param> /// <param name="type"></param> /// <param name="serializer"></param> /// <returns></returns> public static dynamic GetEntityFromRedis(string key, bool isRemove, Type type, ICacheSerializer serializer) { string typeName; string asmName; bool isEntityType; string redisKey; string entityKey = GetEntityTypeFromKey(key, out typeName, ref type, out asmName, out isEntityType, out redisKey); dynamic entity = null; RedisConnectionPool.Process(client => { if (isEntityType) { var data = client.Get <byte[]>(redisKey); if (data != null && type != null) { entity = serializer.Deserialize(data, type); } } else { var data = client.Get <byte[]>(redisKey); if (data != null && type != null) { var dict = (IDictionary)serializer.Deserialize(data, type); entity = dict[entityKey]; } } if (entity == null) { //新版本Hash格式 var data = client.HGet(typeName, RedisConnectionPool.ToByteKey(entityKey)); if (data != null && type != null) { entity = serializer.Deserialize(data, type); } } if (isRemove && entity == null && type != null) { //临时队列删除Entity string setId = (isEntityType ? RedisConnectionPool.EncodeTypeName(typeName) : redisKey) + ":remove"; IDictionary dict = null; RedisConnectionPool.ProcessTrans(client, new string[] { setId }, () => { var data = client.Get <byte[]>(setId); if (data == null) { return(false); } dict = (IDictionary)serializer.Deserialize(data, type); entity = dict[entityKey]; dict.Remove(entityKey); return(true); }, trans => { if (dict != null && dict.Count > 0) { trans.QueueCommand(c => c.Set(setId, serializer.Serialize(dict))); } else { trans.QueueCommand(c => c.Remove(setId)); } }, null); } }); return(entity); }
public void SetUp() { DirectoryUtility.EnsureClearFolder(Folder); _serializer = new CacheSerializer(CacheFilePath); }
/// <summary> /// Start /// </summary> /// <param name="setting"></param> /// <param name="serializer"></param> public static void Start(CacheSetting setting, ICacheSerializer serializer) { _serializer = serializer; _queueWatchTimers = new Timer[DataSyncQueueNum]; for (int i = 0; i < DataSyncQueueNum; i++) { _queueWatchTimers[i] = new Timer(OnCheckRedisSyncQueue, i, 100, 100); } _threadPools = new SmartThreadPool(180 * 1000, 100, 5); _threadPools.Start(); _enableWriteToDb = setting.EnableWriteToDb; if (_enableWriteToDb) { int sqlSyncInterval = ConfigUtils.GetSetting("Game.Cache.UpdateDbInterval", 300 * 1000); _sqlWaitTimers = new Timer[SqlWaitSyncQueueNum]; for (int i = 0; i < SqlWaitSyncQueueNum; i++) { _sqlWaitTimers[i] = new Timer(OnCheckSqlWaitSyncQueue, i, 100, sqlSyncInterval); } SqlStatementManager.Start(); } }
internal void WithSerializer(ICacheSerializer instance) { NotNull(instance, nameof(instance)); this.CacheSerializer = instance; }