コード例 #1
0
        /// <summary>Initializes a new instance of the <see cref="ReviewCollectionDue" /> class.</summary>
        /// <param name="db">Database instance.</param>
        /// <param name="dueCardsLeft">Due card count for currenet session.</param>
        public ReviewCollectionDue(IDatabaseAsync db, int dueCardsLeft) : base(db)
        {
            DueCardsLeft = dueCardsLeft;
            Comparer     = ReviewComparers.DueComparer;

            Initialize(true);
        }
コード例 #2
0
 public RedisMessageTracker(ChatRoom chatRoom, IDatabaseAsync database, int historyLength = 50)
 {
     this.chatRoom      = chatRoom;
     this.database      = database;
     this.historyLength = historyLength;
     this.chatKey       = $"chatRoom:messageHistory:{chatRoom.Id}";
 }
コード例 #3
0
        // $"{_indexCollectionPrefix}[{indexedKey}]";

        public async Task <TIndexedValue[]> GetAsync(IDatabaseAsync context, string indexedKey)
        {
            var setKey     = GenerateSetName(indexedKey);
            var jsonValues = await context.SetMembersAsync(setKey);

            return(jsonValues.Select(jsonValue => _indexedValueReader(jsonValue)).ToArray());
        }
コード例 #4
0
        public IObservable <IDataKey> GetKeys(IDatabaseAsync database, IIndexKey indexes, long start = 0, long stop = -1)
        {
            log.LogTrace("GetKeys");
            var keys = GetIds(database, indexes, start, stop);

            return(keys.Select(item => GetKey(item, indexes)));
        }
コード例 #5
0
 public Worker(ILogger <Worker> logger, MysqlDataAccessSingleton mysqlDataAccess, ConnectionMultiplexer connectionMultiplexer, EtriCommandPublisher publisher)
 {
     _logger = logger;
     this.mysqlDataAccess = mysqlDataAccess;
     redisdatabaseAsync   = connectionMultiplexer.GetDatabase(1);
     this.publisher       = publisher;
 }
コード例 #6
0
        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);
        }
コード例 #7
0
ファイル: RedisClient.cs プロジェクト: AndMu/Wikiled.Redis
 public RedisClient(ILogger <RedisClient> logger, IRedisLink link, IMainIndexManager mainIndexManager, IDatabaseAsync database = null)
 {
     this.link             = link ?? throw new ArgumentNullException(nameof(link));
     this.mainIndexManager = mainIndexManager ?? throw new ArgumentNullException(nameof(mainIndexManager));
     this.logger           = logger ?? throw new ArgumentNullException(nameof(logger));
     this.database         = database;
 }
コード例 #8
0
ファイル: Review.Session.cs プロジェクト: alexis-/Sidekick
        /// <summary>
        ///   Computes the session card review count values asynchronously.
        /// </summary>
        /// <param name="db">Database instance.</param>
        /// <param name="config">The configuration.</param>
        /// <returns></returns>
        public static async Task <ReviewSession> ComputeSessionAsync(
            IDatabaseAsync db, CollectionConfig config)
        {
            int todayStart = DateTime.Today.ToUnixTimestamp();
            int todayEnd   = DateTimeExtensions.Tomorrow.ToUnixTimestamp();

            IEnumerable <ReviewLog> logs =
                await
                db.Table <ReviewLog>()
                .Where(
                    l =>
                    l.Id >= todayStart && l.Id < todayEnd &&
                    (l.LastState == PracticeState.New || l.LastState == PracticeState.Due))
                .SelectColumns(nameof(ReviewLog.LastState))
                .ToListAsync()
                .ConfigureAwait(false);

            int newReviewedToday = logs.Count(l => l.LastState == PracticeState.New);
            int dueReviewedToday = logs.Count() - newReviewedToday;

            int newCount = config.NewCardPerDay - newReviewedToday;
            int dueCount = config.DueCardPerDay - dueReviewedToday;

            return(new ReviewSession(newCount, dueCount));
        }
コード例 #9
0
        private void BeginConnection()
        {
            var config = new ConfigurationOptions()
            {
                ClientName      = "HeroBot_v2_runtime",
                Password        = _config.GetSection("redis").GetSection("auth").GetSection("password").Value,
                DefaultDatabase = int.Parse(_config.GetSection("redis").GetSection("database").Value)
            };

            config.EndPoints.Add($"{_config.GetSection("redis").GetSection("host").Value}:{_config.GetSection("redis").GetSection("port").Value}");
            var redis = ConnectionMultiplexer.Connect(config);

            redis.ConnectionFailed += (sender, evcent) =>
            {
                _loggingService.Log(Discord.LogSeverity.Error, "Can't connect to Redis host");
            };
            redis.ConnectionRestored += (sender, evcent) =>
            {
                _loggingService.Log(Discord.LogSeverity.Warning, "Reconnected to the redis cluster");
            };
            _subscriber = redis.GetSubscriber();
            _loggingService.Log(Discord.LogSeverity.Info, "Connected to redis pub/sub !");
            _database = redis.GetDatabase(int.Parse(_config.GetSection("redis")["database"]), new object { });
            _loggingService.Log(Discord.LogSeverity.Info, "Connected to redis database !");
        }
コード例 #10
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));
        }
コード例 #11
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));
        }
コード例 #12
0
        public async Task <bool> DeleteAsync(IDatabaseAsync redisDatabase, TKey key)
        {
            var headerKey    = _keygen.GetKey(key);
            var chunkDeletes = new List <Task <bool> >();

            using (var redisLock = _lockFactory.GetLockInstance(headerKey))
            {
                await redisLock.AcquireAsync().ConfigureAwait(false);

                var header = await redisDatabase.StringGetAsync(headerKey).ConfigureAwait(false);

                if (header.IsNullOrEmpty)
                {
                    return(false);
                }

                var chunkCount = (int)header;
                for (var chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++)
                {
                    var chunkKey = _keygen.GetKey(key, chunkIndex.ToString());

                    chunkDeletes.Add(redisDatabase.KeyDeleteAsync(chunkKey));
                }

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

                return(await redisDatabase.KeyDeleteAsync(headerKey).ConfigureAwait(false));
            }
        }
コード例 #13
0
 private Task <bool> WriteToRedisAsync(IDatabaseAsync redisDatabase, TKey key, TimeSpan?expiry, string bufferContent, int chunkIndex)
 {
     return(redisDatabase.StringSetAsync(
                _keygen.GetKey(key, chunkIndex.ToString()),
                bufferContent,
                expiry));
 }
コード例 #14
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();
            }));
        }
コード例 #15
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);
        }
コード例 #16
0
        public async Task <TItem> ReadAsync(IDatabaseAsync redisDatabase, TKey key, TimeSpan?resetExpiry)
        {
            var headerKey = _keygen.GetKey(key);
            var chunkGets = new List <Task <RedisValue> >();

            using (var redisLock = _lockFactory.GetLockInstance(headerKey))
            {
                await redisLock.AcquireAsync().ConfigureAwait(false);


                var header = await GetAndUpdateExpiry(redisDatabase, headerKey, resetExpiry).ConfigureAwait(false);

                if (header.IsNullOrEmpty)
                {
                    return(default(TItem));
                }


                var totalChunks = (int)header;

                for (var chunkIndex = 0; chunkIndex < totalChunks; chunkIndex++)
                {
                    var chunkKey = _keygen.GetKey(key, chunkIndex.ToString());

                    chunkGets.Add(GetAndUpdateExpiry(redisDatabase, chunkKey, resetExpiry));
                }

                await Task.WhenAll(chunkGets).ConfigureAwait(false);
            }

            var jsonData = string.Join("", chunkGets.Select(cg => cg.Result));

            return(string.IsNullOrEmpty(jsonData) ? default(TItem) : JsonConvert.DeserializeObject <TItem>(jsonData));
        }
コード例 #17
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));
            }

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

            var key = link.GetKey(dataKey);

            return(Observable.Create <T>(
                       async observer =>
            {
                var items = await link.Resilience.AsyncRetryPolicy.ExecuteAsync(
                    async() =>
                    await database.SortedSetRangeByScoreWithScoresAsync(key, skip: fromRecord, take: toRecord)
                    .ConfigureAwait(false))
                            .ConfigureAwait(false);
                foreach (var value in items)
                {
                    observer.OnNext((T)(object)value);
                }

                observer.OnCompleted();
            }));
        }
コード例 #18
0
 public Task Close()
 {
     _connection.Dispose();
     _connection = null;
     _db         = null;
     return(Task.CompletedTask);
 }
コード例 #19
0
        // readonly UdpClient udpClient = new UdpClient(10000);

        public Worker(ILogger <Worker> logger, IRedisDataAccessor _redis, IPacketQueue queue)
        {
            _logger = logger;
            logger.LogInformation("entry worker");
            redisDataAccessor = _redis;
            db          = _redis.Connection().GetDatabase();
            packetQueue = queue;
        }
コード例 #20
0
        public Task DeleteAll(IDatabaseAsync database, IDataKey key)
        {
            log.LogDebug("DeleteAll: [{0}]", key);
            var tasks = new List <Task>(mainIndexManager.Delete(database, key));

            tasks.Add(link.DeleteAll(database, key));
            return(Task.WhenAll(tasks));
        }
コード例 #21
0
        /// <summary>
        /// `JSON.STRLEN`
        ///
        /// Report the length of the JSON String at `path` in `key`.
        ///
        /// `path` defaults to root if not provided. If the `key` or `path` do not exist, null is returned.
        ///
        /// https://oss.redislabs.com/rejson/commands/#jsonstrlen
        /// </summary>
        /// <param name="db"></param>
        /// <param name="key">The key of the JSON object you need string length information about.</param>
        /// <param name="path">The path of the JSON string you want the length of. This defaults to root.</param>
        /// <param name="commandFlags">Optional command flags.</param>
        /// <returns>Integer, specifically the string's length.</returns>
        public static async Task <int?[]> JsonStringLengthAsync(this IDatabaseAsync db, RedisKey key, string path = ".",
                                                                CommandFlags commandFlags = CommandFlags.None)
        {
            var result = await db.ExecuteAsync(JsonCommands.STRLEN, CombineArguments(key, path), flags : commandFlags)
                         .ConfigureAwait(false);

            return(NullableIntArrayFrom(result));
        }
コード例 #22
0
 public override void Remove(IDatabaseAsync context, IEnumerable <TValue> items)
 {
     foreach (var item in items)
     {
         context.SetRemoveAsync(GenerateSetName(IndexedKeyExtractor.ExtractKey(item)),
                                IndexedValueExtractor(item));
     }
 }
コード例 #23
0
        /// <summary>
        /// `JSON.TOGGLE`
        ///
        /// Toggle the boolean property of a JSON object.
        ///
        /// Official documentation forthcoming.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="key">The key of the JSON object that contains the property that you'd like to toggle.</param>
        /// <param name="path">The path to the boolean property on JSON object that you'd like to toggle.</param>
        /// <param name="commandFlags">Optional command flags.</param>
        /// <returns></returns>
        public static async Task <bool> JsonToggleAsync(this IDatabaseAsync db, RedisKey key, string path,
                                                        CommandFlags commandFlags = CommandFlags.None)
        {
            var result = await db.ExecuteAsync(JsonCommands.TOGGLE, new object[] { key, path }, flags : commandFlags)
                         .ConfigureAwait(false);

            return(bool.Parse(result.ToString()));
        }
コード例 #24
0
        /// <summary>Initializes a new instance of the <see cref="ReviewCollectionNew" /> class.</summary>
        /// <param name="db">Database instance</param>
        /// <param name="config">Current session collection configuration</param>
        /// <param name="newCardsLeft">New cards count for current session</param>
        public ReviewCollectionNew(IDatabaseAsync db, CollectionConfig config, int newCardsLeft)
            : base(db)
        {
            NewCardsLeft = newCardsLeft;
            Random       = config.InsertionOption == CardOrderingOption.Random;

            Initialize(true);
        }
 public DistributedCancellationProcessor(ILockFactory lockFactory, ILogger logger, ISubscriber redisSubscriber, DistributedCancellationConfiguration distributedCancellationConfiguration, IDatabaseAsync databaseAsync)
 {
     _lockFactory     = lockFactory;
     _logger          = logger;
     _redisSubscriber = redisSubscriber;
     _distributedCancellationConfiguration = distributedCancellationConfiguration;
     _databaseAsync = databaseAsync;
 }
コード例 #26
0
        public EventStore(string connectionString, string dbname)
        {
            Guard.AgainstNullArgument(nameof(connectionString), connectionString);
            Guard.AgainstNullArgument(nameof(dbname), dbname);

            _db     = ConnectionMultiplexer.Connect(connectionString).GetDatabase();
            _dbname = dbname;
        }
コード例 #27
0
        /// <summary>
        /// `JSON.CLEAR`
        ///
        /// Clear/empty arrays and objects (to have zero slots/keys without deleting the array/object) returning the count
        /// of cleared paths (ignoring non-array and non-objects paths).
        ///
        /// Official documentation forthcoming.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="key"></param>
        /// <param name="path"></param>
        /// <param name="commandFlags">Optional command flags.</param>
        /// <returns></returns>
        public static async Task <int> JsonClearAsync(this IDatabaseAsync db, RedisKey key, string path,
                                                      CommandFlags commandFlags = CommandFlags.None)
        {
            var result = await db.ExecuteAsync(JsonCommands.CLEAR, new object[] { key, path }, flags : commandFlags)
                         .ConfigureAwait(false);

            return((int)result);
        }
コード例 #28
0
        /// <summary>
        /// Evaluates this LoadedLuaScript against the given database, extracting parameters for the passed in object if any.
        ///
        /// This method sends the SHA1 hash of the ExecutableScript instead of the script itself.  If the script has not
        /// been loaded into the passed Redis instance it will fail.
        /// </summary>
        public Task <RedisResult> EvaluateAsync(IDatabaseAsync db, object ps = null, RedisKey?withKeyPrefix = null, CommandFlags flags = CommandFlags.None)
        {
            RedisKey[]   keys;
            RedisValue[] args;
            Original.ExtractParameters(ps, withKeyPrefix, out keys, out args);

            return(db.ScriptEvaluateAsync(Hash, keys, args, flags));
        }
コード例 #29
0
 public async static Task <ThrottleResult> ThrottleAsync(
     this IDatabaseAsync db, RedisKey key, int maxBurst,
     int maxPerInterval,
     int intervalSeconds = 60, int count = 1)
 {
     return(new ThrottleResult(await db.ExecuteAsync("CL.THROTTLE",
                                                     key, maxBurst.Boxed(), maxPerInterval.Boxed(), intervalSeconds.Boxed(), count.Boxed())));
 }
コード例 #30
0
 public EventSubscribeWorker(ISessionFactory sessionFactory, ConnectionMultiplexer redisConnection)
 {
     this.sessionFactory  = sessionFactory;
     this.redisConnection = redisConnection;
     redisDb   = this.redisConnection.GetDatabase(1);
     Publisher = new EventPublisherWorker();
     Publisher.Initialize();
 }
コード例 #31
0
 public async Task Init(Logger logger, IProviderConfiguration config, string providerName, int numQueues)
 {
     _logger = logger;
     _redisListBaseName = $"orleans-{providerName}-queue";
     ReadRedisConnectionParams(config);
     _connection = await ConnectionMultiplexer.ConnectAsync(_server);
     _database = _connection.GetDatabase(_databaseNum);
     logger.AutoInfo($"connection to Redis successful.");
     IsInitialised = true;
 }
コード例 #32
0
        /// <summary>
        /// Evaluates this LoadedLuaScript against the given database, extracting parameters for the passed in object if any.
        /// 
        /// This method sends the SHA1 hash of the ExecutableScript instead of the script itself.  If the script has not
        /// been loaded into the passed Redis instance it will fail.
        /// </summary>
        public Task<RedisResult> EvaluateAsync(IDatabaseAsync db, object ps = null, RedisKey? withKeyPrefix = null, CommandFlags flags = CommandFlags.None)
        {
            RedisKey[] keys;
            RedisValue[] args;
            Original.ExtractParameters(ps, withKeyPrefix, out keys, out args);

            return db.ScriptEvaluateAsync(Hash, keys, args, flags);
        }
コード例 #33
0
 private Task<bool> Clear(IDatabaseAsync database, Feature feature)
 {
     return database.KeyDeleteAsync(feature.Key);
 }
コード例 #34
0
ファイル: Publisher.cs プロジェクト: censorjam/redis.rx
 public Publisher(IDatabaseAsync db, ISubscriber subscriber)
 {
     _db = db;
     _req = new SubscriptionObservableFactory(subscriber);
 }