internal Task <bool> KeyExpire(IDatabaseAsync database, RedisKey key, CommandFlags flags)
 {
     if (IsDateTime)
     {
         return(database.KeyExpireAsync(key, this.dateTime, flags));
     }
     else
     {
         return(database.KeyExpireAsync(key, this.timeSpan, flags));
     }
 }
        public async Task <bool> HashSetAsync(RedisKey key, RedisValue hashField, RedisValue value, TimeSpan?expiry = null, When when = When.Always)
        {
            key = $"{productID}_{key}";
            IDatabaseAsync db      = null;
            ITransaction   context = CallContext.GetData(KEY_WORD_REDIS_CONTEXT) as ITransaction;

            if (context == null)
            {
                db = Database();
                if (db == null)
                {
                    return(false);
                }
            }
            else
            {
                db = context;
            }
            UsingCount();
            bool is_success = await db.HashSetAsync(key, hashField, value, when);

            if (expiry != null)
            {
                await db.KeyExpireAsync(key, expiry);
            }
            return(is_success);
        }
Beispiel #3
0
        private static async Task SetAsync(IDatabaseAsync cache, string key, object value, double expirationTimeInMinutes)
        {
            await cache.StringSetAsync(key, Serialize(value)).ConfigureAwait(false);

            // We will default to a five minute expiration
            await cache.KeyExpireAsync(key, TimeSpan.FromMinutes(expirationTimeInMinutes)).ConfigureAwait(false);
        }
 public async Task <RedisValue> GetValueAsync(string key)
 {
     if (await _redis.KeyExistsAsync(key))
     {
         await _redis.KeyExpireAsync(key, TimeSpan.FromSeconds(120));
     }
     return(await _redis.StringGetAsync(key));
 }
Beispiel #5
0
        private static async Task <RedisValue> GetAndUpdateExpiry(IDatabaseAsync connection, RedisKey key, TimeSpan?resetExpiry)
        {
            if (resetExpiry.HasValue)
            {
                await connection.KeyExpireAsync(key, resetExpiry.Value, CommandFlags.FireAndForget).ConfigureAwait(false);
            }

            return(await connection.StringGetAsync(key).ConfigureAwait(false));
        }
        public override void Set(IDatabaseAsync context, IEnumerable <TValue> items)
        {
            var indexEntries = items.ToSets(IndexedKeyExtractor.ExtractKey, IndexedValueExtractor).ToArray();

            foreach (var entry in indexEntries)
            {
                context.SetAddAsync(GenerateSetName(entry.Key), entry.ToArray());
                context.KeyExpireAsync(entry.Key, Expiry);
            }
        }
Beispiel #7
0
        public async Task <bool> Synchronize(string path)
        {
            if (await _database.StringIncrementAsync(path) != 1)
            {
                return(false);
            }
            await _database.KeyExpireAsync(path, DateTime.Now.AddMinutes(5));

            return(true);
        }
        public async Task <TItem> ReadAsync(IDatabaseAsync redisDatabase, TKey key, TimeSpan?resetExpiry)
        {
            var redisKey = _keygen.GetKey(key);
            var item     = await redisDatabase.StringGetAsync(redisKey).ConfigureAwait(false);

            if (resetExpiry.HasValue)
            {
                await redisDatabase.KeyExpireAsync(redisKey, resetExpiry.Value, CommandFlags.FireAndForget).ConfigureAwait(false);
            }

            return(string.IsNullOrEmpty(item) ? default(TItem) : JsonConvert.DeserializeObject <TItem>(item));
        }
Beispiel #9
0
        public override void Set(
            IDatabaseAsync context,
            IEnumerable <TValue> items
            )
        {
            var indexName = _indexCollectionName;

            context.HashSetAsync(indexName, items.ToHashEntries(IndexedKeyExtractor.ExtractKey, IndexedValueExtractor));

            if (Expiry.HasValue)
            {
                context.KeyExpireAsync(indexName, Expiry);
            }
        }
        public async Task SetExpire(IDatabaseAsync database, IDataKey key, DateTime dateTime)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

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

            logger.LogDebug("SetExpire: [{0}] - {1}", key, dateTime);
            var keys = await GetAllKeys(database, key).ConfigureAwait(false);

            var tasks = keys.Select(item => database.KeyExpireAsync(item, dateTime));
            await Task.WhenAll(tasks).ConfigureAwait(false);
        }
Beispiel #11
0
        public static Task SetExpire(this IRedisLink link, IDatabaseAsync database, IDataKey key, DateTime dateTime)
        {
            if (link == null)
            {
                throw new ArgumentNullException(nameof(link));
            }

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

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

            var actualKey = link.GetKey(key);

            return(database.KeyExpireAsync(actualKey, dateTime));
        }
Beispiel #12
0
        /// <summary>
        ///     Using HashSetAsync
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        private static async Task GenerateClocks(IDatabaseAsync db)
        {
            var sw     = Stopwatch.StartNew();
            var random = new Random();

            for (var i = 0; i < Total; i++)
            {
                var id    = $"GT-{i}";
                var clock = new ClockInfo
                {
                    DeviceId     = id,
                    SerialNumber = id.ToLower(),
                    ClientId     = random.Next(10000, 99999),
                    ClockGroupId = Guid.NewGuid()
                };
                await db.HashSetAsync($"clock:{id}", clock.ToHashEntries());

                await db.KeyExpireAsync($"clock:{id}", DateTime.Now.AddMinutes(5));
            }

            Console.WriteLine(
                $"Storing {Total} Using original HashSetAsync (v1): {Math.Round(sw.Elapsed.TotalMilliseconds, 3)}ms");
        }
Beispiel #13
0
        private static TupleList <string, Task> RunStackExchangeAsync(string prefix, IDatabaseAsync db)
        {
            var tasks = new TupleList <string, Func <Task> >()
            {
                { "DebugObjectAsync", () => db.DebugObjectAsync($"{prefix}DebugObjectAsync") },
                { "ExecuteAsync", () => db.ExecuteAsync("DDCUSTOM", "COMMAND") },
                { "GeoAddAsync", () => db.GeoAddAsync($"{prefix}GeoAddAsync", new GeoEntry(1.5, 2.5, "member")) },
                { "GeoDistanceAsync", () => db.GeoDistanceAsync($"{prefix}GeoDistanceAsync", "member1", "member2") },
                { "GeoHashAsync", () => db.GeoHashAsync($"{prefix}GeoHashAsync", "member") },
                { "GeoPositionAsync", () => db.GeoPositionAsync($"{prefix}GeoPositionAsync", "member") },
                { "GeoRadiusAsync", () => db.GeoRadiusAsync($"{prefix}GeoRadiusAsync", "member", 2.3) },
                { "GeoRemoveAsync", () => db.GeoRemoveAsync($"{prefix}GeoRemoveAsync", "member") },
                { "HashDecrementAsync", () => db.HashDecrementAsync($"{prefix}HashDecrementAsync", "hashfield", 4.5) },
                { "HashDeleteAsync", () => db.HashDeleteAsync($"{prefix}HashDeleteAsync", "hashfield") },
                { "HashExistsAsync", () => db.HashExistsAsync($"{prefix}HashExistsAsync", "hashfield") },
                { "HashGetAllAsync", () => db.HashGetAllAsync($"{prefix}HashGetAllAsync") },
                { "HashIncrementAsync", () => db.HashIncrementAsync($"{prefix}HashIncrementAsync", "hashfield", 1.5) },
                { "HashKeysAsync", () => db.HashKeysAsync($"{prefix}HashKeysAsync") },
                { "HashLengthAsync", () => db.HashLengthAsync($"{prefix}HashLengthAsync") },
                { "HashSetAsync", () => db.HashSetAsync($"{prefix}HashSetAsync", new HashEntry[] { new HashEntry("x", "y") }) },
                { "HashValuesAsync", () => db.HashValuesAsync($"{prefix}HashValuesAsync") },
                { "HyperLogLogAddAsync", () => db.HyperLogLogAddAsync($"{prefix}HyperLogLogAddAsync", "value") },
                { "HyperLogLogLengthAsync", () => db.HyperLogLogLengthAsync($"{prefix}HyperLogLogLengthAsync") },
                { "HyperLogLogMergeAsync", () => db.HyperLogLogMergeAsync($"{prefix}HyperLogLogMergeAsync", new RedisKey[] { "key1", "key2" }) },
                { "IdentifyEndpointAsync", () => db.IdentifyEndpointAsync() },
                { "KeyDeleteAsync", () => db.KeyDeleteAsync("key") },
                { "KeyDumpAsync", () => db.KeyDumpAsync("key") },
                { "KeyExistsAsync", () => db.KeyExistsAsync("key") },
                { "KeyExpireAsync", () => db.KeyExpireAsync("key", DateTime.Now) },
                // () => db.KeyMigrateAsync("key", ???)
                { "KeyMoveAsync", () => db.KeyMoveAsync("key", 1) },
                { "KeyPersistAsync", () => db.KeyPersistAsync("key") },
                { "KeyRandomAsync", () => db.KeyRandomAsync() },
                { "KeyRenameAsync", () => db.KeyRenameAsync("key1", "key2") },
                { "KeyRestoreAsync", () => db.KeyRestoreAsync("key", new byte[] { 0, 1, 2, 3, 4 }) },
                { "KeyTimeToLiveAsync", () => db.KeyTimeToLiveAsync("key") },
                { "KeyTypeAsync", () => db.KeyTypeAsync("key") },
                { "ListGetByIndexAsync", () => db.ListGetByIndexAsync("listkey", 0) },
                { "ListInsertAfterAsync", () => db.ListInsertAfterAsync("listkey", "value1", "value2") },
                { "ListInsertBeforeAsync", () => db.ListInsertBeforeAsync("listkey", "value1", "value2") },
                { "ListLeftPopAsync", () => db.ListLeftPopAsync("listkey") },
                { "ListLeftPushAsync", () => db.ListLeftPushAsync("listkey", new RedisValue[] { "value3", "value4" }) },
                { "ListLengthAsync", () => db.ListLengthAsync("listkey") },
                { "ListRangeAsync", () => db.ListRangeAsync("listkey") },
                { "ListRemoveAsync", () => db.ListRemoveAsync("listkey", "value3") },
                { "ListRightPopAsync", () => db.ListRightPopAsync("listkey") },
                { "ListRightPopLeftPushAsync", () => db.ListRightPopLeftPushAsync("listkey", "listkey2") },
                { "ListRightPushAsync", () => db.ListRightPushAsync("listkey", new RedisValue[] { "value5", "value6" }) },
                { "ListSetByIndexAsync", () => db.ListSetByIndexAsync("listkey", 0, "value7") },
                { "ListTrimAsync", () => db.ListTrimAsync("listkey", 0, 1) },
                { "LockExtendAsync", () => db.LockExtendAsync("listkey", "value7", new TimeSpan(0, 0, 10)) },
                { "LockQueryAsync", () => db.LockQueryAsync("listkey") },
                { "LockReleaseAsync", () => db.LockReleaseAsync("listkey", "value7") },
                { "LockTakeAsync", () => db.LockTakeAsync("listkey", "value8", new TimeSpan(0, 0, 10)) },
                { "PublishAsync", () => db.PublishAsync(new RedisChannel("channel", RedisChannel.PatternMode.Auto), "somemessage") },
                // { "ScriptEvaluateAsync", () => db.ScriptEvaluateAsync(}
                { "SetAddAsync", () => db.SetAddAsync("setkey", "value1") },
                { "SetCombineAndStoreAsync", () => db.SetCombineAndStoreAsync(SetOperation.Union, "setkey", new RedisKey[] { "value2" }) },
                { "SetCombineAsync", () => db.SetCombineAsync(SetOperation.Union, new RedisKey[] { "setkey1", "setkey2" }) },
                { "SetContainsAsync", () => db.SetContainsAsync("setkey", "value1") },
                { "SetLengthAsync", () => db.SetLengthAsync("setkey") },
                { "SetMembersAsync", () => db.SetMembersAsync("setkey") },
                { "SetMoveAsync", () => db.SetMoveAsync("setkey1", "setkey2", "value2") },
                { "SetPopAsync", () => db.SetPopAsync("setkey1") },
                { "SetRandomMemberAsync", () => db.SetRandomMemberAsync("setkey") },
                { "SetRandomMembersAsync", () => db.SetRandomMembersAsync("setkey", 2) },
                { "SetRemoveAsync", () => db.SetRemoveAsync("setkey", "value2") },
                { "SortAndStoreAsync", () => db.SortAndStoreAsync("setkey2", "setkey") },
                { "SortAsync", () => db.SortAsync("setkey") },
                { "SortedSetAddAsync", () => db.SortedSetAddAsync("ssetkey", new SortedSetEntry[] { new SortedSetEntry("value1", 1.5), new SortedSetEntry("value2", 2.5) }) },
                { "SortedSetCombineAndStoreAsync", () => db.SortedSetCombineAndStoreAsync(SetOperation.Union, "ssetkey1", "ssetkey2", "ssetkey3") },
                { "SortedSetDecrementAsync", () => db.SortedSetDecrementAsync("ssetkey", "value1", 1) },
                { "SortedSetIncrementAsync", () => db.SortedSetIncrementAsync("ssetkey", "value2", 1) },
                { "SortedSetLengthAsync", () => db.SortedSetLengthAsync("ssetkey") },
                { "SortedSetLengthByValueAsync", () => db.SortedSetLengthByValueAsync("ssetkey", "value1", "value2") },
                { "SortedSetRangeByRankAsync", () => db.SortedSetRangeByRankAsync("ssetkey") },
                { "SortedSetRangeByRankWithScoresAsync", () => db.SortedSetRangeByRankWithScoresAsync("ssetkey") },
                { "SortedSetRangeByScoreAsync", () => db.SortedSetRangeByScoreAsync("ssetkey") },
                { "SortedSetRangeByScoreWithScoresAsync", () => db.SortedSetRangeByScoreWithScoresAsync("ssetkey") },
                { "SortedSetRangeByValueAsync", () => db.SortedSetRangeByValueAsync("ssetkey") },
                { "SortedSetRankAsync", () => db.SortedSetRankAsync("ssetkey", "value1") },
                { "SortedSetRemoveAsync", () => db.SortedSetRemoveAsync("ssetkey", "value1") },
                { "SortedSetRemoveRangeByRankAsync", () => db.SortedSetRemoveRangeByRankAsync("ssetkey", 0, 1) },
                { "SortedSetRemoveRangeByScoreAsync", () => db.SortedSetRemoveRangeByScoreAsync("ssetkey", 0, 1) },
                { "SortedSetRemoveRangeByValueAsync", () => db.SortedSetRemoveRangeByValueAsync("ssetkey", "value1", "value2") },
                { "SortedSetScoreAsync", () => db.SortedSetScoreAsync("ssestkey", "value1") },
                { "StringAppendAsync", () => db.StringAppendAsync("ssetkey", "value1") },
                { "StringBitCountAsync", () => db.StringBitCountAsync("ssetkey") },
                { "StringBitOperationAsync", () => db.StringBitOperationAsync(Bitwise.And, "ssetkey1", new RedisKey[] { "ssetkey2", "ssetkey3" }) },
                { "StringBitPositionAsync", () => db.StringBitPositionAsync("ssetkey1", true) },
                { "StringDecrementAsync", () => db.StringDecrementAsync("key", 1.45) },
                { "StringGetAsync", () => db.StringGetAsync("key") },
                { "StringGetBitAsync", () => db.StringGetBitAsync("key", 3) },
                { "StringGetRangeAsync", () => db.StringGetRangeAsync("key", 0, 1) },
                { "StringGetSetAsync", () => db.StringGetSetAsync("key", "value") },
                { "StringGetWithExpiryAsync", () => db.StringGetWithExpiryAsync("key") },
                { "StringIncrementAsync", () => db.StringIncrementAsync("key", 1) },
                { "StringLengthAsync", () => db.StringLengthAsync("key") },
                { "StringSetAsync", () => db.StringSetAsync(new KeyValuePair <RedisKey, RedisValue>[] { new KeyValuePair <RedisKey, RedisValue>("key", "value") }) },
                { "StringSetBitAsync", () => db.StringSetBitAsync("key", 0, true) },
                { "StringSetRangeAsync", () => db.StringSetRangeAsync("key", 3, "value") },
            };

            var pending = new TupleList <string, Task>();

            foreach (var item in tasks)
            {
                try
                {
                    pending.Add(item.Item1, item.Item2());
                }
                catch (Exception e)
                {
                    while (e.InnerException != null)
                    {
                        e = e.InnerException;
                    }
                    Console.WriteLine($"{e.Message}");
                }
            }

            return(pending);
        }
Beispiel #14
0
        /// <summary>
        /// 更新键的失效时间
        /// </summary>
        /// <param name="key">缓存的键</param>
        /// <param name="timeExpire">过期时长,单位秒</param>
        /// <returns></returns>
        public async Task UpdateTTL(string key, int timeExpire)
        {
            IDatabaseAsync db = connection.GetDatabase(DB_INDEX);

            await db.KeyExpireAsync(key, TimeSpan.FromSeconds(timeExpire));
        }
Beispiel #15
0
 /// <summary>
 /// Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is said to be volatile in Redis terminology.
 /// </summary>
 /// <param name="expiry">The timeout to set.</param>
 /// <returns>`true` - if the timeout was set. `false` - if key does not exist or the timeout could not be set.</returns>
 /// <remarks>If key is updated before the timeout has expired, then the timeout is removed as if the PERSIST command was invoked on key.
 /// For Redis versions &lt; 2.1.3, existing timeouts cannot be overwritten. So, if key already has an associated timeout, it will do nothing and return 0. Since Redis 2.1.3, you can update the timeout of a key. It is also possible to remove the timeout using the PERSIST command. See the page on key expiry for more information.</remarks>
 /// <remarks>https://redis.io/commands/expire</remarks>
 /// <remarks>https://redis.io/commands/pexpire</remarks>
 /// <remarks>https://redis.io/commands/persist</remarks>
 public Task <bool> ExpireAsync(TimeSpan?expiry)
 {
     return(RedisDb.KeyExpireAsync(KeyName, expiry));
 }