Example #1
0
        /// <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);
        }