Exemple #1
0
        /// <summary>
        /// 获取缓存,数据类型为String
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Get(string key)
        {
            var db     = RedisCacheConnection.CreateInstance().Database;
            var result = db.StringGet(key);

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// 获取缓存,数据类型为String
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task <string> GetAsync(string key)
        {
            var db     = RedisCacheConnection.CreateInstance().Database;
            var result = await db.StringGetAsync(key);

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// 批量设置缓存,数据类型为String
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="nodes"></param>
        /// <returns></returns>
        public List <bool> Set <T>(List <CacheNode <T> > nodes)
        {
            var db = RedisCacheConnection.CreateInstance().Database;

            var batch = db.CreateBatch();

            var tasks = new List <Task <bool> >();

            for (var m = 0; m < nodes.Count; m++)
            {
                var json = JsonConvert.SerializeObject(nodes[m].Data);

                if (nodes[m].CacheTime != default(TimeSpan))
                {
                    tasks.Add(batch.StringSetAsync(nodes[m].Key, json, nodes[m].CacheTime));
                }
                else
                {
                    tasks.Add(batch.StringSetAsync(nodes[m].Key, json));
                }
            }

            batch.Execute();

            Task.WaitAll(tasks.ToArray());

            var result = new List <bool>();

            foreach (var t in tasks)
            {
                result.Add(t.Result);
            }

            return(result);
        }
Exemple #4
0
        /// <summary>
        /// 批量设置缓存,数据类型为Hash,切莫一次性缓存大量数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="nodes"></param>
        /// <returns></returns>
        public async Task SetHashAsync <T>(List <CacheNode <T> > nodes)
        {
            if (nodes.Count > 65535)
            {
                throw new Exception("Hi guys, i can't digest all those data at one time! may be you can try 'SetAsync' ");
            }

            await Task.Run(() =>
            {
                var db          = RedisCacheConnection.CreateInstance().Database;
                var batch       = db.CreateBatch();
                var currentTime = DateTime.Now;
                for (var m = 0; m < nodes.Count; m++)
                {
                    var maps = ToMap(nodes[m].Data);

                    if (nodes[m].CacheTime != default(TimeSpan))
                    {
                        maps.Add("_ExpiryTime_", currentTime.Add(nodes[m].CacheTime).ToString());
                    }

                    foreach (var map in maps)
                    {
                        batch.HashSetAsync(nodes[m].Key, map.Key, map.Value);
                    }
                }
                batch.Execute();
            });
        }
Exemple #5
0
        /// <summary>
        /// 获取缓存,数据类型为Hash
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task <Dictionary <string, string> > GetHashAsync(string key)
        {
            var items = new Dictionary <string, string>();

            var db     = RedisCacheConnection.CreateInstance().Database;
            var result = await db.HashGetAllAsync(key);

            var kvps = result.ToDictionary();

            if (kvps.Count == 0)
            {
                return(default);
Exemple #6
0
        /// <summary>
        /// 设置缓存,数据类型为String
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="node"></param>
        /// <returns></returns>
        public async Task <bool> SetAsync <T>(CacheNode <T> node)
        {
            var db   = RedisCacheConnection.CreateInstance().Database;
            var json = JsonConvert.SerializeObject(node.Data);

            if (node.CacheTime != default(TimeSpan))
            {
                return(await db.StringSetAsync(node.Key, json, node.CacheTime));
            }
            else
            {
                return(await db.StringSetAsync(node.Key, json));
            }
        }
Exemple #7
0
 /// <summary>
 /// 缓存链接单例,
 /// </summary>
 /// <returns></returns>
 public static RedisCacheConnection CreateInstance()
 {
     if (instance == null)
     {
         _connectionLock.Wait();
         try
         {
             if (instance == null)
             {
                 //设置比较大的线程池,据说能避免Timeout 陷阱
                 ThreadPool.SetMaxThreads(100, 100);
                 instance   = new RedisCacheConnection();
                 Connection = ConnectionMultiplexer.Connect(ConnectionString);
                 Connection.PreserveAsyncOrder = false;
                 instance.Database             = Connection.GetDatabase(-1, null);
             }
         }
         finally
         {
             _connectionLock.Release();
         }
     }
     else
     {
         if (Connection == null || !Connection.IsConnected)
         {
             _connectionLock.Wait();
             try
             {
                 if (Connection == null || !Connection.IsConnected)
                 {
                     ThreadPool.SetMaxThreads(100, 100);
                     Connection = ConnectionMultiplexer.Connect(ConnectionString);
                     Connection.PreserveAsyncOrder = false;
                     instance.Database             = Connection.GetDatabase(-1, null);
                 }
             }
             finally
             {
                 _connectionLock.Release();
             }
         }
     }
     return(instance);
 }
Exemple #8
0
        /// <summary>
        /// 设置缓存,数据类型为Hash
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="node"></param>
        /// <returns></returns>
        public async Task SetHashAsync <T>(CacheNode <T> node)
        {
            await Task.Run(() =>
            {
                var db    = RedisCacheConnection.CreateInstance().Database;
                var batch = db.CreateBatch();
                var maps  = ToMap(node.Data);

                if (node.CacheTime != default(TimeSpan))
                {
                    var currentTime = DateTime.Now;
                    maps.Add("_ExpiryTime_", currentTime.Add(node.CacheTime).ToString());
                }

                foreach (var map in maps)
                {
                    batch.HashSetAsync(node.Key, map.Key, map.Value);
                }

                batch.Execute();
            });
        }
Exemple #9
0
        /// <summary>
        /// 批量设置缓存,数据类型为String
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="nodes"></param>
        /// <returns></returns>
        public async Task SetAsync <T>(List <CacheNode <T> > nodes)
        {
            await Task.Run(() =>
            {
                var db    = RedisCacheConnection.CreateInstance().Database;
                var batch = db.CreateBatch();
                for (var m = 0; m < nodes.Count; m++)
                {
                    var json = JsonConvert.SerializeObject(nodes[m].Data);

                    if (nodes[m].CacheTime != default(TimeSpan))
                    {
                        batch.StringSetAsync(nodes[m].Key, json, nodes[m].CacheTime);
                    }
                    else
                    {
                        batch.StringSetAsync(nodes[m].Key, json);
                    }
                }
                batch.Execute();
            });
        }
Exemple #10
0
        /// <summary>
        /// 判断key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task <bool> ExistsAsync(string key)
        {
            var db = RedisCacheConnection.CreateInstance().Database;

            return(await db.KeyExistsAsync(key));
        }
Exemple #11
0
        /// <summary>
        /// 判断key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Exists(string key)
        {
            var db = RedisCacheConnection.CreateInstance().Database;

            return(db.KeyExists(key));
        }