zoukankan      html  css  js  c++  java
  • StackExchange.Redis.DLL 操作redis简化版

    直接引入StackExchange.Redis.dll来操作redis

    using Newtonsoft.Json;
    using StackExchange.Redis;
    using System;
    using System.Collections.Generic;
    using System.Web.Script.Serialization;
    
    namespace Common
    {
        public class RedisHelper
        {
    #if FALSE
            RedisHelper redislper = new RedisHelper();
            使用事例
            if (redislper.KeyIsExist(item.Equipmentid, redisadress))//判断是否存在key值
                    {
                        var collection = redislper.GetCache<T>(key值);//获取缓存数据                  
                    }  
            //写入缓存
            List<T> list = new  List<T>();
             redislper.SetCache<T>(key值, list);
    #endif
    
            private string adress = "127.0.0.1:6379";
            public RedisHelper() {}
            public void SetCache<T>(string key, List<T> value, DateTime? date = null)
            {
                using (var redis = ConnectionMultiplexer.Connect(adress))
                {
                    //写入
                    var db = redis.GetDatabase();
                    db.StringSet(key, JsonConvert.SerializeObject(value));
                    //设置过期日期
                    if (date != null)
                    {
                        DateTime time = DateTime.Now.AddSeconds(20);
                        db.KeyExpire("key", time);
                    }
                }
    
            }
            /// <summary>
            /// 获取指定key的List
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public List<T> GetCache<T>(string key)
            {
                //var adress = @"127.0.0.1:6379";
                using (var redis = ConnectionMultiplexer.Connect(adress))
                {
                    //读取
                    var db = redis.GetDatabase();
                    return ConvetList<T>(db.StringGet(key));
                }
    
            }
            /// <summary>
            /// 看key是否存在
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public bool KeyIsExist(string key)
            {
                using (var redis = ConnectionMultiplexer.Connect(adress))
                {
                    //读取
                    var db = redis.GetDatabase();
                    return db.KeyExists(key);
                }
    
    
            }
            public List<T> ConvetList<T>(string JsonStr)
            {
                JavaScriptSerializer Serializer = new JavaScriptSerializer();
                List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
                return objs;
    
            }
        }
    }
  • 相关阅读:
    [Codeforces-div.1 809C] Find a car
    [Codeforces-div.1 55D] Beautiful numbers
    [BZOJ3598] [Scoi2014]方伯伯的商场之旅
    [BZOJ3131] [Sdoi2013]淘金
    [BZOJ2757] [SCOI2012]Blinker的仰慕者
    [BZOJ3329] Xorequ
    [POJ3744] Scout YYF I
    angular-file-upload 回显已上传的文件
    angular-file-upload 限制文件上传个数 获取已上传文件队列
    angular-file-upload 一款好用的文件上传组件,基本应用
  • 原文地址:https://www.cnblogs.com/macT/p/11288527.html
Copyright © 2011-2022 走看看