Esempio n. 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Put(CachePutParameters putParameters)
        {
            var key = putParameters.Key;
            var value = putParameters.Value;

            if (key == null)
                throw new ArgumentNullException("key", "null key not allowed");
            if (value == null)
                throw new ArgumentNullException("value", "null value not allowed");
            if (Log.IsDebugEnabled)
                Log.DebugFormat("setting value for item {0}", key);

            try
            {
                using (var disposable = new PooledRedisClientManager.DisposablePooledClient<SerializingRedisClient>(ClientManager))
                {
                    var client = disposable.Client;
                    var bytes = client.Serialize(value);
                    //do transactioned get of generation and value
                    //if it succeeds, and null is returned, then either the key doesn't exist or
                    // our generation is out of date. In the latter case , update generation and try
                    // again.
                    long generationFromServer = CacheNamespace.GetGeneration();
                    while (true)
                    {
                        using (var trans = client.CreateTransaction())
                        {
                            trans.QueueCommand(r => ((IRedisNativeClient)r).SetEx(CacheNamespace.GlobalCacheKey(key),
                                                                _expiry, bytes));

                            //add key to globalKeys set for this namespace
                            trans.QueueCommand(r => r.AddItemToSet(CacheNamespace.GetGlobalKeysKey(),
                                                                CacheNamespace.GlobalCacheKey(key)));

                            trans.QueueCommand(r => r.GetValue(CacheNamespace.GetGenerationKey()),
                                                             x => generationFromServer = Convert.ToInt64(x));
                            trans.Commit();
                        }
                        if (generationFromServer != CacheNamespace.GetGeneration())
                        {
                            //update cached generation value, and try again
                            CacheNamespace.SetGeneration(generationFromServer);
                        }
                        else
                            break;
                    }
                }
            }
            catch (Exception)
            {
                Log.WarnFormat("could not get: {0}", key);
                throw;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="putParameters"></param>
        public void Put(CachePutParameters putParameters)
        {
            var key = putParameters.Key;
            var value = putParameters.Value;

            if (key == null)
                throw new ArgumentNullException("key", "null key not allowed");
            if (value == null)
                throw new ArgumentNullException("value", "null value not allowed");
            if (Log.IsDebugEnabled)
                Log.DebugFormat("setting value for item {0}", key);

            try
            {
                using (var disposable = new PooledRedisClientManager.DisposablePooledClient<SerializingRedisClient>(ClientManager))
                {
                    var client = disposable.Client;
                    var globalKey = CacheNamespace.GlobalCacheKey(key);

                    using (var pipe = client.CreatePipeline())
                    {
                        pipe.QueueCommand(r => ((IRedisNativeClient)r).SetEx(globalKey, _expiry, client.Serialize(value)));

                        pipe.Flush();
                    }
                }
            }
            catch (Exception)
            {
                Log.WarnFormat("could not put {0} for key {1}", value, key);
                throw;
            }
        }