コード例 #1
0
ファイル: RedisLock.cs プロジェクト: iNuoers/Architect
        /// <summary>
        ///
        /// </summary>
        /// <param name="redisClient"></param>
        /// <param name="keyList"></param>
        /// <returns></returns>
        private bool LockItemArr(RedisNativeClient redisClient, List <string> keyList)
        {
            bool result = false;

            try
            {
                //利用MSetNx命令设置锁的值,返回true则表示获得锁并且将值设置成过期时间
                byte[][] value = keyList.Select(x => System.Text.Encoding.UTF8.GetBytes(x)).ToArray <byte[]>();
                var      nx    = redisClient.MSetNx(keyList.ToArray(), value);
                if (nx)
                {
                    foreach (var key in keyList)
                    {
                        redisClient.Expire(key, 600);// 设置redis key过期时间10分钟
                    }
                    result = true;
                }
            }
            catch (Exception exc)
            {
                Logger.Error("RedisLock exception. ", exc);
                throw new RedisResponseException(string.Format("{0} {1}", exc.Message, exc.StackTrace));
            }

            return(result);
        }
コード例 #2
0
 public bool ExpireAt(string keyName, int expireInSeconds)
 {
     using (var client = new RedisNativeClient(ConfigurationManager.AppSettings["RedisServer"]))
     {
         return(client.Expire(keyName, expireInSeconds));
     }
 }
コード例 #3
0
ファイル: Redis.cs プロジェクト: radtek/Shop
        /// <summary>
        /// 设置key 的值和保存时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="session_message"></param>
        /// <param name="expire_minutes"></param>
        public static void SetSession(string key, object session_message, int expire_minutes)
        {
            if (key == "-1")
            {
                return;
            }
            string redis_key = "SESS_" + key;

            byte[] bytes = Encoding.UTF8.GetBytes((string)JsonHelper.ToJson(session_message));
            using (RedisNativeClient RNC = GetNativeClientForKeySpace(space))
            {
                RNC.Set(redis_key, bytes);
                RNC.Expire(redis_key, Convert.ToInt32(new TimeSpan(0, expire_minutes, 0).TotalSeconds));
            }
        }