コード例 #1
0
        public Task AddRecord(IDatabaseAsync database, IDataKey objectKey, params T[] instances)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

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

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

            log.LogDebug("AddRecords: {0}", instances.Length);
            if (instances.Length > 1)
            {
                throw new ArgumentOutOfRangeException();
            }

            var instance  = instances[0];
            var tasks     = new List <Task>();
            var actualKey = link.GetKey(objectKey);
            var entries   = objectSerialization.GetEntries(instance).ToArray();

            tasks.Add(database.HashSetAsync(actualKey, entries));
            tasks.AddRange(mainIndexManager.Add(database, objectKey));
            return(Task.WhenAll(tasks));
        }
コード例 #2
0
        public IObservable <T> GetRecords(IDatabaseAsync database, IDataKey dataKey, long fromRecord = 0, long toRecord = -1)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

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

            var key = link.GetKey(dataKey);

            return(Observable.Create <T>(
                       async observer =>
            {
                var items = await link.Resilience.AsyncRetryPolicy.ExecuteAsync(async() => await redisSetList.GetRedisValues(database, key, fromRecord, toRecord).ConfigureAwait(false))
                            .ConfigureAwait(false);
                var values = GetValues(key, items);
                foreach (var value in values)
                {
                    observer.OnNext(value);
                }

                observer.OnCompleted();
            }));
        }
コード例 #3
0
        public Task SaveItems(IDatabaseAsync database, IDataKey key, params RedisValue[] redisValues)
        {
            var redisKey = link.GetKey(key);

            logger.LogTrace("SaveItems: <{0}>", key);

            var tasks = new List <Task>(mainIndexManager.Add(database, key));
            var size  = GetLimit(key);

            if (size.HasValue)
            {
                var list = redisValues.ToList();
                list.Add(size.Value);
                tasks.Add(database.ScriptEvaluateAsync(
                              link.Generator.GenerateInsertScript(true, redisValues.Length),
                              new[] { redisKey },
                              list.ToArray()));
            }
            else
            {
                tasks.Add(database.ListRightPushAsync(redisKey, redisValues));
            }

            return(Task.WhenAll(tasks));
        }
コード例 #4
0
        public Task AddRecords(IDatabaseAsync database, IEnumerable <IDataKey> keys, params T[] instances)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

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

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

            if (typeof(T) != typeof(SortedSetEntry))
            {
                throw new ArgumentOutOfRangeException(nameof(T));
            }

            var tasks   = new List <Task>();
            var entries = instances.Cast <SortedSetEntry>().ToArray();

            foreach (var key in keys)
            {
                var redisKey = link.GetKey(key);
                var saveTask = database.SortedSetAddAsync(
                    redisKey,
                    entries);

                tasks.AddRange(mainIndexManager.Add(database, key));
                tasks.Add(saveTask);
            }

            return(Task.WhenAll(tasks));
        }
コード例 #5
0
        public static RedisKey GetIndexKey(this IRedisLink link, IIndexKey index)
        {
            if (link == null)
            {
                throw new ArgumentNullException(nameof(link));
            }

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

            return(string.IsNullOrEmpty(index.RepositoryKey) ? link.GetKey(index.Key) : link.GetKey($"{index.RepositoryKey}:{index.Key}"));
        }
コード例 #6
0
ファイル: RedisSet.cs プロジェクト: AndMu/Wikiled.Redis
        public Task SaveItems(IDatabaseAsync database, IDataKey key, params RedisValue[] redisValues)
        {
            var redisKey = link.GetKey(key);

            logger.LogTrace("SaveItems: <{0}>", key);

            var time     = DateTime.UtcNow.ToUnixTime();
            var saveTask = database.SortedSetAddAsync(
                redisKey,
                redisValues.Select(item => new SortedSetEntry(item, time)).ToArray());

            List <Task> tasks = new List <Task>(indexManager.Add(database, key));

            tasks.Add(saveTask);
            return(Task.WhenAll(tasks));
        }
コード例 #7
0
        public static async Task Reindex(this IRedisLink link, ILoggerFactory factory, IDataKey key)
        {
            if (link == null)
            {
                throw new ArgumentNullException(nameof(link));
            }

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

            var log = factory.CreateLogger("IndexManagerExtension");

            log.LogDebug("Redindex {0}", key);
            var manager = new IndexManagerFactory(factory, link);
            var tasks   = new List <Task>();
            var total   = 0;

            foreach (var index in key.Indexes)
            {
                var indexManagers = manager.Create(index);

                tasks.Add(indexManagers.Reset(link.Database, index));

                await Task.WhenAll(tasks.ToArray()).ConfigureAwait(false);

                var actualKey = (string)link.GetKey(key);
                var mask      = Regex.Replace(actualKey, $"{FieldConstants.Object}:.*", $"{FieldConstants.Object}*", RegexOptions.IgnoreCase);

                tasks.Clear();
                foreach (var redisKey in link.Multiplexer.GetKeys(mask))
                {
                    total++;
                    var rawId = Regex.Replace(redisKey, $".*:{FieldConstants.Object}:", string.Empty, RegexOptions.IgnoreCase);
                    tasks.Add(indexManagers.AddRawIndex(link.Database, rawId, index));
                }
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            log.LogDebug("ReIndexed {0} {1}", key, total);
        }
コード例 #8
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));
        }
コード例 #9
0
        public static Task DeleteAll(this IRedisLink link, IDatabaseAsync database, IDataKey key)
        {
            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.KeyDeleteAsync(actualKey));
        }
コード例 #10
0
        public static Task <bool> ContainsRecord(this IRedisLink link, IDatabaseAsync database, IDataKey key)
        {
            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.KeyExistsAsync(actualKey));
        }
コード例 #11
0
        public IObservable <T> GetRecords(IDatabaseAsync database, IDataKey dataKey, long fromRecord = 0, long toRecord = -1)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

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

            var key = link.GetKey(dataKey);

            logger.LogDebug("GetRecords: {0} {1}:{2}", key, fromRecord, toRecord);
            return(Observable.Create <T>(
                       async observer =>
            {
                if (!columnsCache.TryGetValue(typeof(T), out RedisValue[] columns))
                {
                    var subKey = link.GetKey("object");
                    columns = objectSerialization.GetColumns()
                              .Select(item => (RedisValue)($"{subKey}:*->" + item))
                              .ToArray();
                    columnsCache[typeof(T)] = columns;
                }

                var exist = await link.Resilience.AsyncRetryPolicy.ExecuteAsync(
                    async() =>
                    await database.KeyExistsAsync(key).ConfigureAwait(false)).ConfigureAwait(false);

                if (!exist)
                {
                    logger.LogDebug("Key doesn't exist: {0}", key);
                    observer.OnCompleted();
                    return;
                }

                var result = await link.Resilience.AsyncRetryPolicy.ExecuteAsync(
                    async() => await
                    database.SortAsync(
                        key,
                        fromRecord,
                        toRecord,
                        Order.Ascending,
                        SortType.Numeric,
                        "nosort",
                        columns).ConfigureAwait(false))
                             .ConfigureAwait(false);
                if (result.Length % columns.Length != 0)
                {
                    logger.LogError(
                        "Result {0} mismatched with requested number of columns {1}",
                        result.Length,
                        columns.Length);
                    observer.OnCompleted();
                    return;
                }

                var values = objectSerialization.GetInstances(result);
                foreach (var value in values)
                {
                    observer.OnNext(value);
                }

                observer.OnCompleted();
            }));