public virtual long GetCount() { var keyStoreFinalKey = MemcachedObjectCacheStrategy.GetKeyStoreKey(this); if (StoreKey && CheckExisted(keyStoreFinalKey, true)) { var keys = Get <List <string> >(keyStoreFinalKey, true); return(keys.Count); } else { return(0); } }
public virtual async Task <long> GetCountAsync() { var keyStoreFinalKey = MemcachedObjectCacheStrategy.GetKeyStoreKey(this); if (StoreKey && CheckExisted(keyStoreFinalKey, true)) { var keys = await GetAsync <List <string> >(keyStoreFinalKey, true).ConfigureAwait(false); return(keys.Count); } else { return(0); } }
public static IRegisterService RegisterCacheMemcached(this IRegisterService registerService, Dictionary <string, int> memcachedConfig, Func <Dictionary <string, int>, IBaseObjectCacheStrategy> memcachedObjectCacheStrategyInstance) { MemcachedObjectCacheStrategy.RegisterServerList(memcachedConfig); //此处先执行一次委托,直接在下方注册结果,提高每次调用的执行效率 IBaseObjectCacheStrategy objectCacheStrategy = memcachedObjectCacheStrategyInstance(memcachedConfig); if (objectCacheStrategy != null) { CacheStrategyFactory.RegisterObjectCacheStrategy(() => objectCacheStrategy);//Memcached } return(registerService); }
public async Task SetAsync(string key, object value, TimeSpan?expiry = null, bool isFullKey = false) { if (string.IsNullOrEmpty(key) || value == null) { return;// TaskExtension.CompletedTask(); } var cacheKey = GetFinalKey(key, isFullKey); //TODO:加了绝对过期时间就会立即失效(再次获取后为null),memcache低版本的bug var newKey = StoreKey ? await CheckExistedAsync(cacheKey, true).ConfigureAwait(false) == false : false; var json = value.SerializeToCache(); if (expiry.HasValue) { await Cache.StoreAsync(StoreMode.Set, cacheKey, json, expiry.Value).ConfigureAwait(false); } else { await Cache.StoreAsync(StoreMode.Set, cacheKey, json, TimeSpan.FromDays(999999) /*不过期*/).ConfigureAwait(false); } //由于 Enyim.Caching 不支持遍历Keys,所以需要单独储存 if (newKey) { var keyStoreFinalKey = MemcachedObjectCacheStrategy.GetKeyStoreKey(this); List <string> keys; if (!await CheckExistedAsync(keyStoreFinalKey, true).ConfigureAwait(false)) { keys = new List <string>(); } else { keys = await GetAsync <List <string> >(keyStoreFinalKey, true).ConfigureAwait(false); } keys.Add(cacheKey); await Cache.StoreAsync(StoreMode.Set, keyStoreFinalKey, keys.SerializeToCache(), TimeSpan.FromDays(999999) /*不过期*/).ConfigureAwait(false); } }
public void Set(string key, object value, TimeSpan?expiry = null, bool isFullKey = false) { if (string.IsNullOrEmpty(key) || value == null) { return; } var cacheKey = GetFinalKey(key, isFullKey); //TODO:加了绝对过期时间就会立即失效(再次获取后为null),memcache低版本的bug var newKey = StoreKey ? !CheckExisted(cacheKey, true) : false; var json = value.SerializeToCache(); if (expiry.HasValue) { Cache.Store(StoreMode.Set, cacheKey, json, expiry.Value); } else { Cache.Store(StoreMode.Set, cacheKey, json); } //由于 Enyim.Caching 不支持遍历Keys,所以需要单独储存 if (newKey) { var keyStoreFinalKey = MemcachedObjectCacheStrategy.GetKeyStoreKey(this); List <string> keys; if (!CheckExisted(keyStoreFinalKey, true)) { keys = new List <string>(); } else { keys = Get <List <string> >(keyStoreFinalKey, true); } keys.Add(cacheKey); Cache.Store(StoreMode.Set, keyStoreFinalKey, keys.SerializeToCache()); } }
public virtual IDictionary <string, object> GetAll() { IDictionary <string, object> data = new Dictionary <string, object>(); if (StoreKey) { //获取所有Key var keyStoreFinalKey = MemcachedObjectCacheStrategy.GetKeyStoreKey(this); if (CheckExisted(keyStoreFinalKey, true)) { var keys = Get <List <string> >(keyStoreFinalKey, true); foreach (var key in keys) { data[key] = Get(key, true); } } } return(data); }
public virtual async Task <IDictionary <string, object> > GetAllAsync() { IDictionary <string, object> data = new Dictionary <string, object>(); if (StoreKey) { //获取所有Key var keyStoreFinalKey = MemcachedObjectCacheStrategy.GetKeyStoreKey(this); if (await CheckExistedAsync(keyStoreFinalKey, true).ConfigureAwait(false)) { var keys = await GetAsync <List <string> >(keyStoreFinalKey, true).ConfigureAwait(false); foreach (var key in keys) { data[key] = Get(key, true); } } } return(data); }
public virtual async Task RemoveFromCacheAsync(string key, bool isFullKey = false) { if (string.IsNullOrEmpty(key)) { return; } var cacheKey = GetFinalKey(key, isFullKey); await Cache.RemoveAsync(cacheKey).ConfigureAwait(false); if (StoreKey) { //移除key var keyStoreFinalKey = MemcachedObjectCacheStrategy.GetKeyStoreKey(this); if (await CheckExistedAsync(keyStoreFinalKey, true).ConfigureAwait(false)) { var keys = await GetAsync <List <string> >(keyStoreFinalKey, true).ConfigureAwait(false); keys.Remove(cacheKey); await Cache.StoreAsync(StoreMode.Set, keyStoreFinalKey, keys.SerializeToCache(), TimeSpan.FromDays(999999) /*不过期*/).ConfigureAwait(false); } } }
public virtual void RemoveFromCache(string key, bool isFullKey = false) { if (string.IsNullOrEmpty(key)) { return; } var cacheKey = GetFinalKey(key, isFullKey); Cache.Remove(cacheKey); if (StoreKey) { //移除key var keyStoreFinalKey = MemcachedObjectCacheStrategy.GetKeyStoreKey(this); if (CheckExisted(keyStoreFinalKey, true)) { var keys = Get <List <string> >(keyStoreFinalKey, true); keys.Remove(cacheKey); Cache.Store(StoreMode.Set, keyStoreFinalKey, keys.SerializeToCache()); } } }
/// <summary> /// 设置连接信息(不立即启用) /// </summary> /// <param name="redisConfigurationString"></param> public static void SetConfigurationOption(string redisConfigurationString) { MemcachedObjectCacheStrategy.RegisterServerList(redisConfigurationString); }
protected MemcachedCacheLock(MemcachedObjectCacheStrategy strategy, string resourceName, string key, int?retryCount, TimeSpan?retryDelay) : base(strategy, resourceName, key, retryCount, retryDelay) { _mamcachedStrategy = strategy; //LockNow();//立即等待并抢夺锁 }