コード例 #1
0
        public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var creationTime = DateTimeOffset.UtcNow;

            var absoluteExpiration = GetAbsoluteExpiration(creationTime, options);

            var result = _redisClient.Eval(SetScript, key,
                                           new object[]
            {
                absoluteExpiration?.Ticks ?? NotPresent,
                options.SlidingExpiration?.Ticks ?? NotPresent,
                GetExpirationInSeconds(creationTime, absoluteExpiration, options) ?? NotPresent,
                value
            });
        }
コード例 #2
0
        /// <summary>
        /// 释放分布式锁。https://github.com/2881099/csredis/blob/master/src/CSRedisCore/CSRedisClient.cs
        /// </summary>
        /// <returns>成功/失败</returns>
        public bool Unlock(string name, string value)
        {
            return(_redisClient.Eval(@"local gva = redis.call('GET', KEYS[1])
if gva == ARGV[1] then
  redis.call('DEL', KEYS[1])
  return 1
end
return 0", name, value)?.ToString() == "1");
        }
コード例 #3
0
        //https://www.cnblogs.com/additwujiahua/p/11479303.html

        /// <summary>
        /// 批处理模糊查询
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        internal static object BatchFuzzyQuery(string key)
        {
            try
            {
                var lau = @" local array = {" + key + "}" +
                          @" local t = { }" +
                          @" for iter, value in ipairs(array) do" +
                          @" local key = redis.call('keys', '*_'..value..'_*');" +
                          @" if #key>0 then" +
                          @" table.insert(t,key[1])" +
                          @" end " +
                          @" end " +
                          @" return  redis.call('mget', unpack(t))";
                return(_redisManager.Eval(lau, "", ""));
            }
            catch (Exception)
            {
                return(null);
            }
        }
コード例 #4
0
        /// <summary>
        /// 解锁
        /// </summary>
        /// <param name="client">redis客户端连接</param>
        /// <param name="key">锁key</param>
        /// <param name="selfMark">对应加锁客户端标识</param>
        /// <returns></returns>
        public static bool M5_UnLock(this CSRedisClient client, string key, string selfMark)
        {
            var script = @"local getLock = redis.call('GET', KEYS[1])
                            if getLock == ARGV[1] then
                              redis.call('DEL', KEYS[1])
                              return 1
                            end
                            return 0";

            return(client.Eval(script, key, selfMark)?.ToString() == "1");
        }
コード例 #5
0
        /// <summary>
        /// 加锁毫秒级
        /// </summary>
        /// <param name="client">redis客户端连接</param>
        /// <param name="key">锁key</param>
        /// <param name="value">锁值</param>
        /// <param name="expireMilliSeconds">缓存时间 单位/毫秒 默认1000毫秒</param>
        /// <returns></returns>
        public static bool M5_LockPE(this CSRedisClient client, string key, object value, int expireMilliSeconds = 1000)
        {
            var script = @"local isNX = redis.call('SETNX', KEYS[1], ARGV[1])
                           if isNX == 1 then
                               redis.call('PEXPIRE', KEYS[1], ARGV[2])
                               return 1
                           end
                           return 0";

            return(client.Eval(script, key, value, expireMilliSeconds)?.ToString() == "1");
        }
コード例 #6
0
        /*
         *  也可以使用CSRedisClient内置方法 lock 和 unlock
         */


        /// <summary>
        /// 加锁
        /// </summary>
        /// <param name="client">redis客户端连接</param>
        /// <param name="key">锁key</param>
        /// <param name="value">锁值</param>
        /// <param name="expireSeconds">缓存时间 单位/秒 默认1秒</param>
        /// <returns></returns>
        public static bool M5_Lock(this CSRedisClient client, string key, object value, int expireSeconds = 1)
        {
            //注意未设置锁的过期时间不解锁就成了死锁了

            var script = @"local isNX = redis.call('SETNX', KEYS[1], ARGV[1])
                           if isNX == 1 then
                               redis.call('EXPIRE', KEYS[1], ARGV[2])
                               return 1
                           end
                           return 0";

            return(client.Eval(script, key, value, expireSeconds)?.ToString() == "1");
        }