Ejemplo n.º 1
0
        /// <summary>
        /// 添加实体
        /// </summary>
        public static void Set <T>(string key, T value, TimeSpan?expiry = null, SetRedisCallback <T> callback = null)
        {
            var db = GetDatabase();

            db?.StringSetAsync(key, JsonConvert.SerializeObject(value), expiry);

            callback?.Invoke(value);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 采用管道批量向Redis中添加数据
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="data">数据集合</param>
        /// <param name="expiry">过期时间</param>
        /// <param name="callback">回调函数</param>
        public static void Set <T>(Dictionary <string, T> data, TimeSpan?expiry = null, SetRedisCallback <Dictionary <string, T> > callback = null)
        {
            var db    = GetDatabase();
            var batch = db.CreateBatch();

            var rows = 0;

            foreach (var item in data)
            {
                rows++;
                if (rows % 1000 == 0)
                {
                    batch.Execute();
                    //数量超过一千,重新开启一个管道
                    batch = db.CreateBatch();
                }
                batch?.StringSetAsync(item.Key, JsonConvert.SerializeObject(item.Value), expiry);
            }
            batch.Execute();

            callback?.Invoke(data);
        }