zoukankan      html  css  js  c++  java
  • C# 操作redis

    下载 redis windows 版本 官网目前没有windows安装下载,下载地址为 

    https://github.com/MicrosoftArchive/redis/releases  

    直接点击安装 一直下一步

    我的安装路径  D:Program FilesRedis

    启动命令行工具 测试redis

    C#  连接redis 在项目类库引用上右键启动NuGet 搜索redis 安装以下二个框架

    redis工具类

    public class RedisCacheHelper
        {
            private static readonly PooledRedisClientManager pool = null;
            private static readonly string[] redisHosts = null;
            public static int RedisMaxReadPool = 3;
            public static int RedisMaxWritePool = 1;
    
            static RedisCacheHelper()
            {
                var redisHostStr = "127.0.0.1:6379";
    
                if (!string.IsNullOrEmpty(redisHostStr))
                {
                    redisHosts = redisHostStr.Split(',');
    
                    if (redisHosts.Length > 0)
                    {
                        pool = new PooledRedisClientManager(redisHosts, redisHosts,
                            new RedisClientManagerConfig()
                            {
                                MaxWritePoolSize = RedisMaxWritePool,
                                MaxReadPoolSize = RedisMaxReadPool,
                                AutoStart = true
                            });
                    }
                }
            }
    
            #region Add
            public static void Add<T>(string key, T value, DateTime expiry)
            {
                if (value == null)
                {
                    return;
                }
    
                if (expiry <= DateTime.Now)
                {
    
                    Remove(key);
    
                    return;
                }
    
                try
                {
                    if (pool != null)
                    {
                        using (var r = pool.GetClient())
                        {
                            if (r != null)
                            {
                                r.SendTimeout = 1000;
                                r.Set(key, value, expiry - DateTime.Now);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);
                }
    
            }
    
    
            public static void Add<T>(string key, T value, TimeSpan slidingExpiration)
            {
                if (value == null)
                {
                    return;
                }
    
                if (slidingExpiration.TotalSeconds <= 0)
                {
                    Remove(key);
    
                    return;
                }
    
                try
                {
                    if (pool != null)
                    {
                        using (var r = pool.GetClient())
                        {
                            if (r != null)
                            {
                                r.SendTimeout = 1000;
                                r.Set(key, value, slidingExpiration);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);
                }
            }
    
            public static T Get<T>(string key)
            {
                if (string.IsNullOrEmpty(key))
                {
                    return default(T);
                }
    
                T obj = default(T);
    
                try
                {
                    if (pool != null)
                    {
                        using (var r = pool.GetClient())
                        {
                            if (r != null)
                            {
                                r.SendTimeout = 1000;
                                obj = r.Get<T>(key);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "获取", key);
                }
    
    
                return obj;
            }
            #endregion
    
            public static void Remove(string key)
            {
                try
                {
                    if (pool != null)
                    {
                        using (var r = pool.GetClient())
                        {
                            if (r != null)
                            {
                                r.SendTimeout = 1000;
                                r.Remove(key);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "删除", key);
                }
    
            }
    
            public static bool Exists(string key)
            {
                try
                {
                    if (pool != null)
                    {
                        using (var r = pool.GetClient())
                        {
                            if (r != null)
                            {
                                r.SendTimeout = 1000;
                                return r.ContainsKey(key);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "是否存在", key);
                }
    
                return false;
            }
        }
    View Code

    测试操作 

     测试

  • 相关阅读:
    vue+axios实现文件下载
    处理 Vue 单页面应用 SEO
    JSON.parse()与JSON.stringify()的区别
    JS设置Cookie过期时间
    <div>标签仿<textarea>。contentEditable=‘true’,赋予非表单标签内容可以编辑
    CSS上下左右居中的几种方法
    jqGrid 修改单元格值或者替换图片及其他
    对于Web性能优化, 了解和经验
    初步了解反射案列
    JS IndexOf移除符合规则的一项
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/10273111.html
Copyright © 2011-2022 走看看