Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        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());
            }
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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);
                }
            }
        }
Ejemplo n.º 8
0
        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());
                }
            }
        }
Ejemplo n.º 9
0
 protected MemcachedCacheLock(MemcachedObjectCacheStrategy strategy, string resourceName, string key, int?retryCount, TimeSpan?retryDelay)
     : base(strategy, resourceName, key, retryCount, retryDelay)
 {
     _mamcachedStrategy = strategy;
 }