示例#1
0
        public override T AddOrMerge(string key, T value)
        {
            if (Type != CacheType.Hash)
            {
                throw new InvalidOperationException("CacheSet is not of type Hash");
            }
            T outputValue = default(T);

            lock (_mergeLock)
            {
                T oldValue = null;
                if (!_dictionary.ContainsKey(key))
                {
                    var fqkey     = _cacheMap.GetKey(_context.Prefix, Name, key);
                    var hashValue = _cachingService.GetHash(fqkey, _cacheMap);
                    if (hashValue != null)
                    {
                        _dictionary.TryAdd(key, hashValue);
                        oldValue = hashValue;
                    }
                }
                else
                {
                    oldValue = _dictionary[key];
                }
                if (oldValue != null)
                {
                    value = _cacheMap.Merge(oldValue, value);
                }
                _dictionary[key] = value;
                outputValue      = value;
            }
            return(outputValue);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="setKey"></param>
        /// <param name="cacheSetElements"></param>
        /// <param name="cacheMap"></param>
        private void CacheSetElements <T>(string setKey, IEnumerable <T> cacheSetElements, ICacheMap <T> cacheMap)
            where T : class
        {
            var key = cacheMap.GetKey(_context.Prefix, setKey);
            //var memberKey = member.GetSubKey(pair.Key.ToString());
            var serializedValues = SerializeSet(cacheSetElements, cacheMap);

            foreach (var sv in serializedValues)
            {
                _cacher.SetAdd(key, sv);
            }
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hashKey"></param>
        /// <param name="hashtable"></param>
        /// <param name="cacheMap"></param>
        private async Task CacheHashes <T>(string hashKey, IDictionary <string, T> hashtable, ICacheMap <T> cacheMap)
            where T : class
        {
            var prefix = $"{_context.Prefix}:{hashKey}";

            foreach (var pair in hashtable)
            {
                var memberKey    = cacheMap.GetKey(prefix, pair.Key);
                var hashElements = SerializeDictionaryElement(pair.Value, cacheMap);
                await _cacher.SetHashesAsync(memberKey, hashElements);
            }
            //var elapsed2 = watch2.ElapsedMilliseconds; Debug.WriteLine($"HASH Cache time: {elapsed2}ms {msSum}");
        }