zoukankan      html  css  js  c++  java
  • [Redis]c# redis缓存辅助类

    复制代码
    using NServiceKit.Redis;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace H5.Utility
    {
        /// <summary>
        /// 分布式缓存
        /// </summary>
        public static class RedisCache
        {
            private static IRedisClient RCClient = null;
            /// <summary>
            /// 设置redis缓存
            /// </summary>
            /// <typeparam name="T">泛型类</typeparam>
            /// <param name="key">缓存键</param>
            /// <param name="value">泛型实体</param>
            /// <param name="expire">过期时间</param>
            /// <returns></returns>
            public static bool Set<T>(string key, T value, DateTime expire)
            {
                try
                {
                    using (RCClient = GetClient())
                    {
                        return RCClient.Set<T>(key, value, expire);
                    }
                }
                catch
                {
                    return false;
                }
            }
            /// <summary>
            /// 获取缓存
            /// </summary>
            /// <typeparam name="T">实体</typeparam>
            /// <param name="key">键值</param>
            /// <returns></returns>
            public static T Get<T>(string key)
            {
                try
                {
                    using (RCClient = GetClient())
                    {
                        return RCClient.Get<T>(key);
                    }
                }
                catch
                {
                    //如果redis出现异常,则直接返回默认值
                    return default(T);
                }
            }
            /// <summary>
            /// 移除缓存
            /// </summary>
            /// <param name="key"></param>
            public static void Remove(string key)
            {
                using (RCClient = GetClient())
                {
                    RCClient.Remove(key);
                }
            }
            /// <summary>
            /// 获取客户端
            /// </summary>
            /// <returns></returns>
            private static IRedisClient GetClient()
            {
                RedisClientFactory factory = RedisClientFactory.Instance;
                RedisClient client = null;
                if (string.IsNullOrEmpty(ConfigManager.RedisServer))
                {
                    throw new ArgumentNullException("redis server ip is empty.");
                }
                if (string.IsNullOrEmpty(ConfigManager.RedisPwd))
                {
                    throw new ArgumentNullException("redis server pwd is empty.");
                }
                client = factory.CreateRedisClient(ConfigManager.RedisServer, ConfigManager.RedisPort);
                client.Password = ConfigManager.RedisPwd;
                client.Db = ConfigManager.RedisServerDb;
                return client;
            }
        }
    }
    复制代码

     用到的程序集

    功能描述

    可以直接缓存实体类,设置过期时间,移除缓存,获取缓存功能。

    使用RedisClientFactory工厂获取redis客户端实例。如果Redis设置了密码,在配置文件中添加修改

                client = factory.CreateRedisClient(ConfigManager.RedisServer, ConfigManager.RedisPort);
                client.Password = ConfigManager.RedisPwd;

    修改redis的ip和端口号,密码即可。

    使用场景

    具体的使用过程中,使用redis的超时可以对数据进行一些持久化管理,对于一些数据一致性不高的数据进行缓存,使得读取速度提高,使用redis集群时可以是用主从复制功能,Redis集群没有中心节点,并且带有复制和故障转移特性,这可以避免单个节点成为性能瓶颈,或者因为某个节点下线而导致整个集群下线。

    转载:博客地址:http://www.cnblogs.com/wolf-sun/

  • 相关阅读:
    Overloaded的方法是否可以改变返回值的类型
    parseXXX的用法
    java的类型转换问题。int a = 123456;short b = (short)a;System.out.println(b);为什么结果是-7616?
    UVA 10405 Longest Common Subsequence(简单DP)
    POJ 1001 Exponentiation(大数处理)
    POJ 2318 TOYS(计算几何)(二分)
    POJ 1265 Area (计算几何)(Pick定理)
    POJ 3371 Flesch Reading Ease (模拟题)
    POJ 3687 Labeling Balls(拓扑序列)
    POJ 1094 Sorting It All Out(拓扑序列)
  • 原文地址:https://www.cnblogs.com/cqqinjie/p/7297960.html
Copyright © 2011-2022 走看看