コード例 #1
0
        /// <summary>
        /// 使用CSRedis代替StackChange.Redis
        /// <remarks>
        /// 关于CSRedis项目,请参考<seealso cref="https://github.com/2881099/csredis"/>
        /// </remarks>
        /// </summary>
        /// <param name="services"></param>
        /// <param name="redisConnectionStrings">redis连接字符串。
        /// <remarks>
        /// 如果是单机模式,请只输入一个连接字符串;如果是集群模式,请输入多个连接字符串
        /// </remarks>
        /// </param>
        /// <returns></returns>
        public static IServiceCollection UseCsRedisClient(this IServiceCollection services, params string[] redisConnectionStrings)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            if (redisConnectionStrings == null || redisConnectionStrings.Length == 0)
            {
                throw new ArgumentNullException(nameof(redisConnectionStrings));
            }
            CSRedisClient redisClient;

            if (redisConnectionStrings.Length == 1)
            {
                //单机模式
                redisClient = new CSRedisClient(redisConnectionStrings[0]);
            }
            else
            {
                //集群模式
                redisClient = new CSRedisClient(null, redisConnectionStrings);
            }
            //初始化 RedisHelper
            RedisHelper.Initialization(redisClient, serialize: value => JsonConvertor.Serialize(value),
                                       deserialize: (data, type) => JsonConvertor.Deserialize(data, type));
            //注册mvc分布式缓存
            services.AddSingleton <IDistributedCache>(new Microsoft.Extensions.Caching.Redis.CSRedisCache(RedisHelper.Instance));
            return(services);
        }
コード例 #2
0
        public void TestCsRedisClient()
        {
            BuildServiceForSqlServer();
            var dbContext = AspectCoreContainer.Resolve <IDbContextCore>();

            RedisHelper.Set("test_cache_key", JsonConvertor.Serialize(dbContext.GetCurrentDatabaseTableList()),
                            10 * 60);
            Thread.Sleep(2000);
            var content = DistributedCacheManager.Get("test_cache_key");

            Assert.IsNotNull(content);
        }
コード例 #3
0
 public static async Task <bool> SetAsync(string key, object data, int expiredSeconds)
 {
     return(await RedisHelper.SetAsync(key, JsonConvertor.Serialize(data), expiredSeconds));
 }
コード例 #4
0
 public static void Set(string key, object data, int expiredSeconds)
 {
     RedisHelper.Set(key, JsonConvertor.Serialize(data), expiredSeconds);
 }
コード例 #5
0
 /// <summary>
 /// 转换对象为JSON格式字符串
 /// </summary>
 /// <param name="obj">需要转换的对象</param>
 /// <returns></returns>
 public static string ToJson(this object obj)
 {
     return(JsonConvertor.Serialize(obj));
 }