示例#1
0
        public async Task <T> GetAllAsync(string entityId)
        {
            var redisKey = PrimaryCacheKeyFormatter(entityId);
            var hash     = (await RedisDatabase.HashGetAllAsync(redisKey).ConfigureAwait(false)).ToList();
            var props    = typeof(T).GetProperties();
            var entity   = new T();

            foreach (var prop in props)
            {
                var propInterfaces = prop.PropertyType.GetInterfaces();

                // If it's a collection property, extract out the values from the hash
                if (CanPutHashItemsIntoCollection(prop, propInterfaces, hash, entity))
                {
                    continue;
                }

                // Else try to get a hash value for the given property.
                var hashItem = hash.FirstOrDefault(h => h.Name == prop.Name);
                if (hashItem == default(HashEntry) || hashItem.Value.IsNull)
                {
                    continue;
                }

                // Deserialize the value
                var val = JsonConvert.DeserializeObject(hashItem.Value, prop.PropertyType);
                prop.SetValue(entity, val);
            }
            return(entity);
        }
示例#2
0
        public void Delegates_to_datastore_creator()
        {
            var model             = Mock.Of <IModel>();
            var connection        = Mock.Of <RedisConnection>();
            var configurationMock = new Mock <DbContextConfiguration>();

            configurationMock.Setup(m => m.Model).Returns(model);
            configurationMock.Setup(m => m.Connection).Returns(connection);

            var creatorMock = new Mock <RedisDataStoreCreator>(configurationMock.Object);

            creatorMock.Setup(m => m.EnsureCreated(model)).Returns(true);
            creatorMock.Setup(m => m.EnsureDeleted(model)).Returns(true);
            configurationMock.Setup(m => m.DataStoreCreator).Returns(creatorMock.Object);

            var database = new RedisDatabase(configurationMock.Object);

            Assert.True(database.EnsureCreated());
            creatorMock.Verify(m => m.EnsureCreated(model), Times.Once);

            Assert.True(database.EnsureDeleted());
            creatorMock.Verify(m => m.EnsureDeleted(model), Times.Once);

            Assert.Same(connection, database.Connection);
        }
示例#3
0
        protected virtual Task[] PipelineSetMany(
            IEnumerable <KeyValuePair <string, byte[]> > items,
            DistributedCacheEntryOptions options)
        {
            items   = Check.NotNull(items, nameof(items));
            options = Check.NotNull(options, nameof(options));

            var itemArray          = items.ToArray();
            var tasks              = new Task[itemArray.Length];
            var creationTime       = DateTimeOffset.UtcNow;
            var absoluteExpiration = GetAbsoluteExpiration(creationTime, options);

            for (var i = 0; i < itemArray.Length; i++)
            {
                tasks[i] = RedisDatabase.ScriptEvaluateAsync(SetScript, new RedisKey[] { Instance + itemArray[i].Key },
                                                             new RedisValue[]
                {
                    absoluteExpiration?.Ticks ?? NotPresent,
                    options.SlidingExpiration?.Ticks ?? NotPresent,
                    GetExpirationInSeconds(creationTime, absoluteExpiration, options) ?? NotPresent,
                    itemArray[i].Value
                });
            }

            return(tasks);
        }
示例#4
0
        protected virtual async Task <byte[][]> GetAndRefreshManyAsync(
            IEnumerable <string> keys,
            bool getData,
            CancellationToken token = default)
        {
            token.ThrowIfCancellationRequested();

            await ConnectAsync(token);

            var keyArray = keys.Select(key => Instance + key).ToArray();

            RedisValue[][] results;

            if (getData)
            {
                results = await RedisDatabase.HashMemberGetManyAsync(keyArray, AbsoluteExpirationKey,
                                                                     SlidingExpirationKey, DataKey);
            }
            else
            {
                results = await RedisDatabase.HashMemberGetManyAsync(keyArray, AbsoluteExpirationKey,
                                                                     SlidingExpirationKey);
            }

            await Task.WhenAll(PipelineRefreshManyAndOutData(keyArray, results, out var bytes));

            return(bytes);
        }
示例#5
0
        public async Task <bool> ExistsAsync(T entity)
        {
            var entityId = PrimaryEntityIdLocator(entity);
            var redisKey = PrimaryCacheKeyFormatter(entityId);

            return(await RedisDatabase.KeyExistsAsync(redisKey).ConfigureAwait(false));
        }
示例#6
0
        public void Register(Container container)
        {
            container.RegisterSingleton<IEventReciever, EventReciever>();
            container.RegisterSingleton<IEventSender, EventSender>();

            var eventDispatcherRegistration = Lifestyle.Singleton.CreateRegistration(() =>
            {
                var rdb = new RedisDatabase(
                    new RedisConnection(ConfigurationManager.ConnectionStrings["RedisServer"].ConnectionString),
                    int.Parse(ConfigurationManager.AppSettings["redisApiResultStorageDb"]));

                var redisStorage = new RedisStorage<AsyncResultContainer<dynamic>>(rdb, null, "result:{0:D}");
                return new EventDispatcher(
                    container.GetInstance<IEventSender>(),
                    redisStorage);
            }, container);
            container.AddRegistration(typeof(EventDispatcher), eventDispatcherRegistration);

            var storageProviderRegistration = Lifestyle.Singleton.CreateRegistration(() => new StorageProvider(new RedisDatabase(
                new RedisConnection(ConfigurationManager.ConnectionStrings["RedisServer"].ConnectionString),
                int.Parse(ConfigurationManager.AppSettings["redisEventStorageDb"]))), container);

            container.AddRegistration(typeof(StorageProvider), storageProviderRegistration);
            container.AddRegistration(typeof(IModelElementStorage), storageProviderRegistration);

            container.RegisterSingleton<ManagerDependencyResolver>(new ManagerDependencyResolver(x => ((IServiceProvider)container).GetService(x)));
            container.RegisterSingleton<ScriptEngine>();
            container.RegisterSingleton<Engine>();
            container.RegisterSingleton<ApplicationProxyBase, EventApplicationProxy>();
        }
        public static bool TryDequeue(string key, out string val)
        {
            if (!IsEnable)
            {
                throw new PlatformNotSupportedException("No Redis enable");
            }
            val = string.Empty;

            if (!RedisDatabase.KeyExists(key))
            {
                return(false);
            }

            //var lockKey = key + "_locker";
            //if (RedisDatabase.KeyExists(lockKey))
            //{
            //    return false;
            //}

            //RedisDatabase.StringSet(lockKey, "true");
            var temp = RedisDatabase.ListRightPop(key);

            //RedisDatabase.KeyDelete(lockKey);

            if (temp.HasValue == false)
            {
                return(false);
            }
            val = temp;


            //val = RedisDatabase.ListRightPop(key);

            return(true);
        }
示例#8
0
 public async Task SetFieldValueAsync <TFieldValue>(string entityId, Expression <Func <T, TFieldValue> > propertyExpression, TFieldValue value)
 {
     var redisKey      = PrimaryCacheKeyFormatter(entityId);
     var hashName      = GetPropertyName(propertyExpression);
     var serializedObj = JsonConvert.SerializeObject(value);
     await RedisDatabase.HashSetAsync(redisKey, hashName, serializedObj).ConfigureAwait(false);
 }
示例#9
0
        protected virtual byte[][] GetAndRefreshMany(
            IEnumerable <string> keys,
            bool getData)
        {
            Connect();

            var keyArray = keys.Select(key => Instance + key).ToArray();

            RedisValue[][] results;

            if (getData)
            {
                results = RedisDatabase.HashMemberGetMany(keyArray, AbsoluteExpirationKey,
                                                          SlidingExpirationKey, DataKey);
            }
            else
            {
                results = RedisDatabase.HashMemberGetMany(keyArray, AbsoluteExpirationKey,
                                                          SlidingExpirationKey);
            }

            Task.WaitAll(PipelineRefreshManyAndOutData(keyArray, results, out var bytes));

            return(bytes);
        }
示例#10
0
        public void Initialize()
        {
            var factory = new DbContextFactory();

            this.db        = new RedisDatabase();
            this.dbContext = factory.Create(this.db);
        }
 public static string HashGet(string key, string fieldName)
 {
     if (!IsEnable)
     {
         throw new PlatformNotSupportedException("No Redis enable");
     }
     return(RedisDatabase.HashGet(key, fieldName));
 }
示例#12
0
        public void RemoveMany(IEnumerable <string> keys)
        {
            keys = Check.NotNull(keys, nameof(keys));

            Connect();

            RedisDatabase.KeyDelete(keys.Select(key => (RedisKey)(Instance + key)).ToArray());
        }
示例#13
0
        /// <summary>
        /// Remove an ambigious object from the pool.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        private void Remove <T>(T obj)
        {
            var t    = typeof(T);
            var guid = t.GetProperty("Identifier").GetValue(obj).ToString();

            RedisDatabase.SortedSetRemove(t.Name, guid);
            RedisDatabase.KeyDelete(guid);
        }
 public static void HashDelete(string key, string fieldName)
 {
     if (!IsEnable)
     {
         throw new PlatformNotSupportedException("No Redis enable");
     }
     RedisDatabase.HashDelete(key, fieldName);
 }
示例#15
0
 /// <summary>
 /// Önbellekte veriyi, verilmişse istenilen süre kadar tutar
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="expireTime"></param>
 public bool Set(string key, string value, TimeSpan?expireTime = null)
 {
     if (expireTime > TimeSpan.MinValue)
     {
         return(RedisDatabase.StringSet(key, value, expireTime));
     }
     return(RedisDatabase.StringSet(key, value));
 }
示例#16
0
        public async Task <HttpActionResult <DeploymentRequest, Deployment> > Post([FromBody] DeploymentRequest request) => await this.WithResponseContainer(
            request,
            async info =>
        {
            if (string.IsNullOrEmpty(request.Name))
            {
                throw new InternalHttpException("Deployment package name must be specified.", (int)HttpStatusCode.BadRequest, new { request });
            }

            if (string.IsNullOrEmpty(request.Version))
            {
                throw new InternalHttpException("Deployment package version must be specified.", (int)HttpStatusCode.BadRequest, new { request });
            }

            if (request.Agents == null || request.Agents.Count == 0)
            {
                throw new InternalHttpException("Agent list must be specified.", (int)HttpStatusCode.BadRequest, new { request });
            }

            IDeserializer deserializer = new DeserializerBuilder().Build();
            var yaml = deserializer.Deserialize <Dictionary <string, List <string> > >(new StringReader(request.Yaml));
            Dictionary <string, List <string> > dict = new Dictionary <string, List <string> >(yaml, StringComparer.InvariantCultureIgnoreCase)
                                                       .ToDictionary(kv => kv.Key, kv => kv.Value ?? new List <string>());
            var deployment = new Deployment
            {
                DeploymentPackage = new DeploymentPackage
                {
                    Name    = request.Name,
                    Version = request.Version
                },
                InstallCommands   = dict.ContainsKey(nameof(Deployment.InstallCommands)) ? dict[nameof(Deployment.InstallCommands)] : new List <string>(),
                StartCommands     = dict.ContainsKey(nameof(Deployment.StartCommands)) ? dict[nameof(Deployment.StartCommands)] : new List <string>(),
                StopCommands      = dict.ContainsKey(nameof(Deployment.StopCommands)) ? dict[nameof(Deployment.StopCommands)] : new List <string>(),
                UninstallCommands = dict.ContainsKey(nameof(Deployment.UninstallCommands)) ? dict[nameof(Deployment.UninstallCommands)] : new List <string>(),
                Timestamp         = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                Id = Guid.NewGuid().ToString()
            };

            if (deployment.InstallCommands.Count == 0 && deployment.StartCommands.Count == 0)
            {
                throw new InternalHttpException("Must specify at least one of install/start commands.", (int)HttpStatusCode.BadRequest, new { request });
            }

            if (deployment.StopCommands.Count == 0 && deployment.UninstallCommands.Count == 0)
            {
                throw new InternalHttpException("Must specify at least one of uninstall/stop commands.", (int)HttpStatusCode.BadRequest, new { request });
            }

            Dictionary <string, AgentInfo> agentsDict   = await RedisDatabase.HashGetAllAsync <AgentInfo>(CacheKeys.AgentInfo);
            Dictionary <string, Deployment> deployments = agentsDict.Values
                                                          .Where(i => i.Status == AgentStatus.Ready)
                                                          .Where(i => request.Agents.Contains(i.Id))
                                                          .Select(i => i.Id)
                                                          .ToDictionary(k => k, v => deployment);

            await RedisDatabase.HashSetAsync(CacheKeys.ActiveDeployments, deployments);
            return(deployment);
        });
        public static void HashSet(string key, KeyValuePair <string, string> val)
        {
            if (!IsEnable)
            {
                throw new PlatformNotSupportedException("No Redis enable");
            }

            RedisDatabase.HashSet(key, val.Key, val.Value);
        }
 public static bool TryEnqueue(string key, params string[] values)
 {
     if (!IsEnable)
     {
         throw new PlatformNotSupportedException("No Redis enable");
     }
     RedisDatabase.ListLeftPush(key, values.ToRedisValueArray());
     return(true);
 }
        public static long QueueLength(string key)
        {
            if (RedisDatabase.KeyExists(key) == false)
            {
                return(0);
            }

            return(RedisDatabase.ListLength(key));
        }
        public static void Set(string key, string val, TimeSpan?expireAfter = null)
        {
            if (!IsEnable)
            {
                return;
            }

            RedisDatabase.StringSet(key, val, expireAfter);
        }
        public static void Set <T>(string key, T val, TimeSpan?expireAfter = null)
        {
            if (!IsEnable)
            {
                return;
            }

            RedisDatabase.StringSet(key, JsonConvert.SerializeObject(val), expireAfter);
        }
示例#22
0
        public RedisTransaction(RedisDatabase wrapped, object asyncState) : base(wrapped.multiplexer, wrapped.Database, asyncState ?? wrapped.AsyncState)
        {
            // need to check we can reliably do this...
            var commandMap = multiplexer.CommandMap;

            commandMap.AssertAvailable(RedisCommand.MULTI);
            commandMap.AssertAvailable(RedisCommand.EXEC);
            commandMap.AssertAvailable(RedisCommand.DISCARD);
        }
示例#23
0
        public async Task RemoveManyAsync(IEnumerable <string> keys, CancellationToken token = default)
        {
            keys = Check.NotNull(keys, nameof(keys));

            token.ThrowIfCancellationRequested();
            await ConnectAsync(token);

            await RedisDatabase.KeyDeleteAsync(keys.Select(key => (RedisKey)(Instance + key)).ToArray());
        }
        public void Initialize()
        {
            this.db = new RedisDatabase();

            var typeRepo = new TypeRepository(new TypeMetadataGenerator(false));

            this.dbRecordBuilder = new DbRecordBuilder(typeRepo);
            this.stub            = new DynamicProxyStub(typeRepo, this.db, this.dbRecordBuilder);
        }
示例#25
0
        /// <summary>
        /// Önbellekte tutulan byte[] tipinde veriyi döner.
        /// </summary>
        /// <param name="key"></param>
        public T GetDeserializeBytes <T>(string key)
        {
            if (Exists(key))
            {
                byte[] obj = RedisDatabase.StringGet(key);
                return(Deserialize <T>(obj));
            }

            return(default(T));
        }
示例#26
0
 public RedisQueryContext(
     [NotNull] IModel model,
     [NotNull] ILogger logger,
     [NotNull] StateManager stateManager,
     [NotNull] RedisDatabase redisDatabase)
     : base(model, logger, stateManager)
 {
     Check.NotNull(redisDatabase, "redisDatabase");
     _redisDatabase = redisDatabase;
 }
示例#27
0
        public void Handle(TreeItemSelectedMessage message)
        {
            if (message?.SelectedItem is RedisDatabase)
            {
                redisDatabase = message.SelectedItem as RedisDatabase;

                DbName   = redisDatabase.GetDatabaseNumber.ToString();
                KeyCount = redisDatabase.GetKeyCount.ToString();
            }
        }
示例#28
0
        public async Task Should_Not_Request_Again_For_Same_Context_Async()
        {
            var context1 = GetNewContextSubstitute();
            var context2 = GetNewContextSubstitute();

            string cacheKey   = Guid.NewGuid().ToString();
            int    counter    = 0;
            int    cacheValue = 1;

            Task <TestCacheItem> GetCacheValue()
            {
                counter++;
                return(Task.FromResult(new TestCacheItem {
                    Value = cacheValue
                }));
            }

            CurrentHttpContext = context1;
            var item1 = await _typedCache.GetAsync(cacheKey, GetCacheValue); //First request

            CurrentHttpContext = context2;
            var item2 = await _typedCache.GetAsync(cacheKey, GetCacheValue); //Second request

            CurrentHttpContext = context1;
            var item3 = await _typedCache.GetAsync(cacheKey, GetCacheValue); //First request again

            CurrentHttpContext = context2;
            var item4 = await _typedCache.GetAsync(cacheKey, GetCacheValue); //Second request again

            await RedisDatabase.Received(2).StringGetAsync(Arg.Any <RedisKey>());

            var cachedObject =
                RedisSerializer.Serialize(new TestCacheItem {
                Value = cacheValue
            }, typeof(TestCacheItem));

            await RedisDatabase.Received(2).StringSetAsync(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(),
                                                           Arg.Any <When>(),
                                                           Arg.Any <CommandFlags>());

            (await _typedCache.GetOrDefaultAsync(cacheKey)).Value.ShouldBe(cacheValue);

            counter.ShouldBe(2);
            item1.ShouldNotBe(null);
            item1.Value.ShouldBe(cacheValue);

            item2.ShouldNotBe(null);
            item2.Value.ShouldBe(cacheValue);

            item3.ShouldNotBe(null);
            item3.Value.ShouldBe(cacheValue);

            item4.ShouldNotBe(null);
            item4.Value.ShouldBe(cacheValue);
        }
示例#29
0
        public async Task Should_Request_Again_For_Different_Contexts_Async()
        {
            RedisDatabase.ClearReceivedCalls();

            string cacheKey   = "Test";
            int    cacheValue = 1;

            int counter = 0;

            Task <MyCacheItem> GetCacheValue()
            {
                counter++;
                return(Task.FromResult(new MyCacheItem {
                    Value = cacheValue
                }));
            }

            RedisDatabase.StringSetAsync(Arg.Any <RedisKey>(), Arg.Any <RedisValue>(), Arg.Any <TimeSpan>()).Returns(true);

            var cachedObject = RedisSerializer.Serialize(new MyCacheItem {
                Value = cacheValue
            }, typeof(MyCacheItem));

            ChangeHttpContext();

            var item = await _perRequestRedisCache.GetAsync(cacheKey, GetCacheValue);

            await RedisDatabase.Received(1).StringGetAsync(Arg.Any <RedisKey>());

            await RedisDatabase.Received(1).StringSetAsync(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(), Arg.Any <CommandFlags>());

            RedisDatabase.StringGetAsync(Arg.Any <RedisKey>()).Returns(Task.FromResult(cachedObject));

            ChangeHttpContext();
            var item1 = await _perRequestRedisCache.GetAsync(cacheKey, GetCacheValue);

            await RedisDatabase.Received(2).StringGetAsync(Arg.Any <RedisKey>());

            ChangeHttpContext();
            var item2 = await _perRequestRedisCache.GetAsync(cacheKey, GetCacheValue);

            await RedisDatabase.Received(3).StringGetAsync(Arg.Any <RedisKey>());

            //should still be one received calls
            await RedisDatabase.Received(1).StringSetAsync(Arg.Any <RedisKey>(), cachedObject, Arg.Any <TimeSpan>(), Arg.Any <When>(), Arg.Any <CommandFlags>());

            counter.ShouldBe(1);
            item.ShouldNotBe(null);
            item.Value.ShouldBe(cacheValue);
            item1.ShouldNotBe(null);
            item1.Value.ShouldBe(cacheValue);
            item2.ShouldNotBe(null);
            item2.Value.ShouldBe(cacheValue);
        }
示例#30
0
        public async Task RemoveMatchKeyAsync(string key, bool?hideErrors, CancellationToken token)
        {
            Check.NotNull(key, nameof(key));

            await ConnectAsync(token);

            RedisDatabase.ScriptEvaluate(@" local keys = redis.call('keys', ARGV[1]) 
                for i=1,#keys,5000 do 
                redis.call('del', unpack(keys, i, math.min(i+4999, #keys)))
                end", values: new RedisValue[] { Instance + key });
        }
        public RedisDataStoreServices(
            [NotNull] RedisDataStore store,
            [NotNull] RedisDataStoreCreator creator,
            [NotNull] RedisConnection connection,
            [NotNull] ValueGeneratorCache valueGeneratorCache,
            [NotNull] RedisDatabase database,
            [NotNull] ModelBuilderFactory modelBuilderFactory)
        {
            Check.NotNull(store, "store");
            Check.NotNull(creator, "creator");
            Check.NotNull(connection, "connection");
            Check.NotNull(valueGeneratorCache, "valueGeneratorCache");
            Check.NotNull(database, "database");
            Check.NotNull(modelBuilderFactory, "modelBuilderFactory");

            _store = store;
            _creator = creator;
            _connection = connection;
            _valueGeneratorCache = valueGeneratorCache;
            _database = database;
            _modelBuilderFactory = modelBuilderFactory;
        }
        public void Delegates_to_datastore_creator()
        {
            var model = Mock.Of<IModel>();
            var connection = Mock.Of<RedisConnection>();
            var configurationMock = new Mock<DbContextConfiguration>();
            configurationMock.Setup(m => m.Model).Returns(model);
            configurationMock.Setup(m => m.Connection).Returns(connection);

            var creatorMock = new Mock<RedisDataStoreCreator>(configurationMock.Object);
            creatorMock.Setup(m => m.EnsureCreated(model)).Returns(true);
            creatorMock.Setup(m => m.EnsureDeleted(model)).Returns(true);
            configurationMock.Setup(m => m.DataStoreCreator).Returns(creatorMock.Object);

            var database = new RedisDatabase(configurationMock.Object);

            Assert.True(database.EnsureCreated());
            creatorMock.Verify(m => m.EnsureCreated(model), Times.Once);

            Assert.True(database.EnsureDeleted());
            creatorMock.Verify(m => m.EnsureDeleted(model), Times.Once);

            Assert.Same(connection, database.Connection);
        }