Ejemplo n.º 1
0
        public void TestConnectivity()
        {
            var redisStore = new RedisStore();
            HttpResponseMessage responseMessage = null;

            Console.WriteLine(redisStore.TryGetValue(new CacheKey("http://google.com", new string[0]), out responseMessage));
        }
Ejemplo n.º 2
0
        private async Task <bool> HasInvalidStatusCode(
            LastChangedRecord record,
            RedisStore redisStore,
            int responseStatusCode,
            string requestUrl,
            HttpResponseMessage response)
        {
            if (_validStatusCodes.Contains(responseStatusCode))
            {
                return(false);
            }

            _logger.LogWarning(
                "Backend call to {RequestUrl} ({AcceptType}) returned statuscode {StatusCode} which was invalid.",
                requestUrl,
                record.AcceptType,
                response.StatusCode);

            record.ErrorCount++;
            record.LastError        = DateTimeOffset.UtcNow;
            record.LastErrorMessage = $"Backend call to {requestUrl} ({record.AcceptType}) returned statuscode {response.StatusCode} which was invalid.";

            if (record.ErrorCount >= _maxErrorCount)
            {
                _logger.LogInformation(
                    "{CacheKey} reached {MaxErrorCount} errors, purging from cache.",
                    record.CacheKey.ToLowerInvariant(),
                    record.ErrorCount);

                await redisStore.DeleteKeyAsync(record.CacheKey.ToLowerInvariant());
            }

            return(true);
        }
Ejemplo n.º 3
0
        static void Main()
        {
            IDatabase db  = RedisStore.getInstance().RedisCacheTable;
            var       sub = db.Multiplexer.GetSubscriber();

            sub.Subscribe(COUNTER_HINTS_CHANNEL, delegate
            {
                // process all messages in queue
                string msg = db.ListRightPop(COUNTER_QUEUE_NAME);
                while (msg != null)
                {
                    UserData userData = JsonConvert.DeserializeObject <UserData>(msg);
                    Console.WriteLine("Region: " + userData.region + " message: " + msg);

                    int countGlasn   = Regex.Matches(userData.message, @"[aiueoy]", RegexOptions.IgnoreCase).Count;
                    int countSoglasn = Regex.Matches(userData.message, @"[bcdfghjklmnpqrstvwxz]", RegexOptions.IgnoreCase).Count;
                    string message   = getStringifyUserDataCounter(userData, countGlasn, countSoglasn);
                    Console.WriteLine("Queue value: " + message);

                    SendMessage(message, db);

                    msg = db.ListRightPop(COUNTER_QUEUE_NAME);
                }
            });
            Console.WriteLine("Observable subscribe vowel cons counter is ready. For exit press Enter.");
            Console.ReadLine();
        }
Ejemplo n.º 4
0
        public async Task OneTimeSetUp()
        {
            //_server.KillAll();
            await _server.Start();

            _multiplexer = ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(RedisConnectionOptions));
            _connection  = new RedisTestDatabaseConnector(_multiplexer);

            _cacheType1 = new RedisStore <TestCompany>(
                new RedisTestDatabaseConnector(_multiplexer),
                new DefaultJsonSerializer(),
                new CollectionSettings <TestCompany> {
                MasterKeyExtractor = new TestCompanyKeyExtractor()
            }
                );

            _cacheType2 = new RedisStore <TestPerson>(
                new RedisTestDatabaseConnector(_multiplexer),
                new DefaultJsonSerializer(),
                new CollectionSettings <TestPerson> {
                MasterKeyExtractor = new TestPersonKeyExtractor()
            });

            _cacheType1WithExpiry = new RedisStore <TestCompany>(
                new RedisTestDatabaseConnector(_multiplexer),
                new DefaultJsonSerializer(),
                new CollectionSettings <TestCompany>
            {
                MasterKeyExtractor = new TestCompanyKeyExtractor(),
                Expiry             = TimeSpan.FromSeconds(1)
            });

            _database = new RedisDatabaseManager(_connection);
        }
Ejemplo n.º 5
0
        public void TestConnectivity()
        {
            var redisStore = new RedisStore(ConnectionString);
            HttpResponseMessage responseMessage = null;

            Console.WriteLine(redisStore.GetValueAsync(new CacheKey("http://google.com", new string[0])).Result);
        }
Ejemplo n.º 6
0
        public void AccessingLocalRedis100TimesLessThanASecond()
        {
            var c = new RedisStore(ConnectionString);

            var sw = Stopwatch.StartNew();

            for (int i = 0; i < 100; i++)
            {
                var buffer = new byte[_r.Next(4 * 1024, 64 * 1024)];
                _r.NextBytes(buffer);
                var key  = new CacheKey(UrlStem + Guid.NewGuid().ToString(), new string[0]);
                var task = c.AddOrUpdateAsync(key,
                                              new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(buffer)
                });
                task.GetAwaiter().GetResult();
                var r = c.GetValueAsync(key).GetAwaiter().GetResult();
            }

            if (sw.ElapsedMilliseconds > 1000)
            {
                throw new Exception("Took more than 1000 ms");
            }
        }
Ejemplo n.º 7
0
        static void Main()
        {
            RedisStore instance = RedisStore.getInstance();
            IDatabase  db       = instance.RedisCacheTable;
            var        sub      = db.Multiplexer.GetSubscriber();

            sub.Subscribe(RATE_HINTS_CHANNEL, delegate
            {
                // process all messages in queue
                string msg = db.ListRightPop(RATE_QUEUE_NAME);
                while (msg != null)
                {
                    Console.WriteLine(msg);
                    UserDataCounter userDataCounter = JsonConvert.DeserializeObject <UserDataCounter>(msg);

                    double relation = userDataCounter.countSoglasn == 0 ?
                                      0 : (double)userDataCounter.countGlasn / userDataCounter.countSoglasn;
                    string message = GetStringifyUserDataRater(userDataCounter, relation);
                    var idDb       = (int)db.StringGet($"RANK_{userDataCounter.id}");

                    instance.RedisCache(idDb).StringSet($"RANK_{userDataCounter.id}", message);

                    msg = db.ListRightPop(RATE_QUEUE_NAME);

                    Console.WriteLine(GetStringifyTextStatisticConsRater(userDataCounter.id, relation));

                    sub.Publish(EVENTS, GetStringifyTextStatisticConsRater(userDataCounter.id, relation));
                }
            });
            Console.WriteLine("Observable subscribe vowel cons rater is ready. For exit press Enter.");
            Console.ReadLine();
        }
Ejemplo n.º 8
0
        public RedisBus(IConfiguration configuration, IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            var redisStore = new RedisStore(configuration);

            _redisDatabase = redisStore.RedisCache;
        }
Ejemplo n.º 9
0
        public async Task MainAsync()
        {
            // You should dispose a service provider created using ASP.NET
            // when you are finished using it, at the end of your app's lifetime.
            // If you use another dependency injection framework, you should inspect
            // its documentation for the best way to do this.
            using (var services = ConfigureServices())
            {
                var client = services.GetRequiredService <DiscordSocketClient>();

                client.Log += LogAsync;
                services.GetRequiredService <CommandService>().Log += LogAsync;

                // Tokens should be considered secret data and never hard-coded.
                // We can read from the environment variable to avoid hardcoding.
                await client.LoginAsync(TokenType.Bot, RedisStore.GetSecret("KK2_BOT"));

                await client.StartAsync();

                // Here we initialize the logic required to register our commands.
                await services.GetRequiredService <CommandHandlingService>().InitializeAsync();

                await Task.Delay(Timeout.Infinite);
            }
        }
Ejemplo n.º 10
0
        public DeviceHubService(ILogger <DeviceHubService> logger,
                                IDeviceHistoryRepository deviceHistoryRepository,
                                IDeviceService deviceService,
                                IHubContext <WebHubService> webHubContext,
                                RedisStore rediscache,
                                IBackgroundTaskQueue queue,
                                BadDisconnectSocketService badDisconnectSocketService,
                                IMemoryCache memoryCache,
                                INotificationSender notificationSenderExtention)
        {
            _logger = logger ??
                      throw new ArgumentNullException(nameof(logger));
            _deviceHistoryRepository = deviceHistoryRepository ??
                                       throw new ArgumentNullException(nameof(deviceHistoryRepository));
            _deviceService = deviceService ??
                             throw new ArgumentNullException(nameof(deviceService));
            _webHubContext = webHubContext ??
                             throw new ArgumentNullException(nameof(webHubContext));
            _rediscache = rediscache.RedisCache ??
                          throw new ArgumentNullException(nameof(rediscache));
            Queue = queue ??
                    throw new ArgumentNullException(nameof(queue));
            _notificationSenderExtention = notificationSenderExtention ??
                                           throw new ArgumentNullException(nameof(notificationSenderExtention));

            _badDisconnectSocketService = badDisconnectSocketService ??
                                          throw new ArgumentNullException(nameof(badDisconnectSocketService));
            _memoryCache = memoryCache ??
                           throw new ArgumentNullException(nameof(memoryCache));
        }
Ejemplo n.º 11
0
        public NotificationSenderTests(BaseTest baseTest)
        {
            _baseTest = baseTest ?? throw new ArgumentNullException(nameof(baseTest));
            var loggerMock = new Mock <ILogger <NotificationSender> >();

            cache = _baseTest.redisStore;
            _notificationSender = new NotificationSender(_baseTest._notificationSenderExtentionOptions, cache, loggerMock.Object);
        }
Ejemplo n.º 12
0
        public GivenABatchWasAlreadyStarted()
        {
            _sut = new RedisStore(
                RedisFixture.MockConnectionMultiplexer(RedisFixture.MockRedisBatch().Object),
                ETagFixture.MockETagGenerator());

            _sut.CreateBatch();
        }
Ejemplo n.º 13
0
 public MeteoritesController(IHttpClientFactory clientFactory, Meteorite.MeteoriteClient meteoriteClient,
                             TracerFactory tracerFactory, RedisStore redisStore, ILogger <WeatherForecastController> logger)
 {
     ClientFactory   = clientFactory;
     TracerFactory   = tracerFactory;
     RedisStore      = redisStore;
     Logger          = logger;
     MeteoriteClient = meteoriteClient;
 }
 public MeteoriteServiceImpl(RedisStore redisStore, MessageBus messageBus, IHttpClientFactory clientFactory,
                             TracerFactory tracerFactory, IConfiguration configuration, ILogger <MeteoriteServiceImpl> logger)
 {
     _redisStore    = redisStore;
     _messageBus    = messageBus;
     _clientFactory = clientFactory;
     _tracerFactory = tracerFactory;
     _configuration = configuration;
     _logger        = logger;
 }
Ejemplo n.º 15
0
        public void Redis_cache_empty_remove()
        {
            //Arrange
            var cache = new RedisStore(REDIS_LOCALHOST);

            //Act
            cache.Remove(string.Empty);

            //Assert
            Assert.NotNull(cache);
        }
Ejemplo n.º 16
0
        public static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Not enough arguments. Must set count limit words");
                return;
            }

            var  instance        = RedisStore.getInstance();
            var  db              = instance.RedisCacheTable;
            var  sub             = db.Multiplexer.GetSubscriber();
            var  limitCountWords = int.Parse(args[0]);
            var  countWords      = 0;
            bool status;

            sub.Subscribe(EVENTS, (channel, message) =>
            {
                string mes       = message;
                string eventName = mes.Split("=>")[0];
                if (eventName == TEXT_CREATED_EVENT)
                {
                    var contextId = mes.Split("=>")[1];
                    countWords++;
                    status = countWords <= limitCountWords;
                    sub.Publish(EVENTS, PublishMessage(contextId, status));
                    if (!status)
                    {
                        Task.Run(async() => {
                            await Task.Delay(60000);
                            countWords = 0;
                            Console.WriteLine("Reset!");
                        });
                    }
                }

                if (eventName == TEXT_RANK_CALCULATED_EVENT)
                {
                    string mesVal = mes.Split("=>")[1];
                    TextStatisticConsRater textStatisticConsRater = JsonConvert.DeserializeObject <TextStatisticConsRater>(mesVal);
                    Console.WriteLine(message);
                    if (textStatisticConsRater.rank <= 0.5)
                    {
                        countWords--;
                    }
                }
            });


            Console.WriteLine(countWords);
            Console.WriteLine("Observable subscribe text processing limiter is ready. For exit press Enter.");
            Console.ReadLine();
        }
Ejemplo n.º 17
0
        public void Redis_cache_empty_get_with_ConfigOptions()
        {
            //Arrange
            var cache = new RedisStore(ConfigurationOptions.Parse(REDIS_LOCALHOST));

            //Act
            bool found = cache.TryGetValue(string.Empty, out var response);

            //Assert
            Assert.NotNull(cache);
            Assert.False(found);
            Assert.Null(response);
        }
Ejemplo n.º 18
0
        public void GetValue()
        {
            var redisStore = new RedisStore(ConnectionString);
            var client     = new HttpClient(new CachingHandler(redisStore)
            {
                InnerHandler = new HttpClientHandler()
            });

            var httpResponseMessage = client.GetAsync(CacheableResource1).Result;
            var response            = redisStore.GetValueAsync(new CacheKey(CacheableResource1, new string[0])).Result;

            Assert.NotNull(response);
        }
Ejemplo n.º 19
0
        public void IfNotThrowThenDoesNot()
        {
            var s = new RedisStore("NoneExisting", throwExceptions: false);
            var k = new CacheKey("https://google.com/", new string[0]);
            var r = new HttpResponseMessage(HttpStatusCode.Accepted);

            s.AddOrUpdateAsync(k, r).Wait();
            var r2      = s.GetValueAsync(k).Result;
            var removed = s.TryRemoveAsync(k).Result;

            Assert.Null(r2);
            Assert.False(removed);
        }
Ejemplo n.º 20
0
        public void GetValue()
        {
            var redisStore = new RedisStore();
            var client = new HttpClient(new CachingHandler(redisStore)
            {
                InnerHandler = new HttpClientHandler()
            });

            var httpResponseMessage = client.GetAsync(CacheableResource1).Result;
            HttpResponseMessage response = null;
            var tryGetValue = redisStore.TryGetValue(new CacheKey(CacheableResource1, new string[0]), out response);
            Assert.That(tryGetValue);
            Assert.IsNotNull(response);
        }
Ejemplo n.º 21
0
        public void GetLastAccessed()
        {
            var redisStore = new RedisStore();
            var client = new HttpClient(new CachingHandler(redisStore)
            {
                InnerHandler = new HttpClientHandler()
            });

            var httpResponseMessage = client.GetAsync(CacheableResource1).Result;
            CacheItemMetadata metadata = redisStore.GetEarliestAccessedItem("ajax.googleapis.com");
            Assert.IsNotNull(metadata);
            Assert.AreEqual("ajax.googleapis.com", metadata.Domain);
            Assert.Less(DateTime.Now.Subtract(TimeSpan.FromSeconds(10)), metadata.LastAccessed);
        }
Ejemplo n.º 22
0
        public void Redis_cache_empty_key()
        {
            //Arrange
            var cache      = new RedisStore(REDIS_LOCALHOST);
            var expiration = new TimeSpan(0, 1, 0);

            var response = A.Fake <FakeCachedResponse>();

            //Act
            cache.Set(string.Empty, response, expiration);

            //Assert
            Assert.NotNull(cache);
            Assert.NotNull(response);
        }
Ejemplo n.º 23
0
        public void GetValue()
        {
            var redisStore = new RedisStore();
            var client     = new HttpClient(new CachingHandler(redisStore)
            {
                InnerHandler = new HttpClientHandler()
            });

            var httpResponseMessage      = client.GetAsync(CacheableResource1).Result;
            HttpResponseMessage response = null;
            var tryGetValue = redisStore.TryGetValue(new CacheKey(CacheableResource1, new string[0]), out response);

            Assert.That(tryGetValue);
            Assert.IsNotNull(response);
        }
Ejemplo n.º 24
0
        public void WorksWithMaxAgeZeroAndStillStoresIt()
        {
            var redisStore = new RedisStore(ConnectionString);
            var client     = new HttpClient(new CachingHandler(redisStore)
            {
                InnerHandler       = new HttpClientHandler(),
                DefaultVaryHeaders = new string[0]
            });

            var httpResponseMessage = client.GetAsync(MaxAgeZeroResource).Result;
            var key      = new CacheKey(MaxAgeZeroResource, new string[0]);
            var response = redisStore.GetValueAsync(key).Result;

            Assert.NotNull(response);
        }
Ejemplo n.º 25
0
        public void GetLastAccessed()
        {
            var redisStore = new RedisStore();
            var client     = new HttpClient(new CachingHandler(redisStore)
            {
                InnerHandler = new HttpClientHandler()
            });

            var httpResponseMessage    = client.GetAsync(CacheableResource1).Result;
            CacheItemMetadata metadata = redisStore.GetEarliestAccessedItem("ajax.googleapis.com");

            Assert.IsNotNull(metadata);
            Assert.AreEqual("ajax.googleapis.com", metadata.Domain);
            Assert.Less(DateTime.Now.Subtract(TimeSpan.FromSeconds(10)), metadata.LastAccessed);
        }
Ejemplo n.º 26
0
        public static void Main(string[] args)
        {
            int startingSecurityId = 0;
            int numberOfSecurities = NUMBER_OF_SECURITIES_TO_CREATE;

            if (args != null && args.Length > 0)
            {
                int.TryParse(args[0], out numberOfSecurities);
                if (args.Length > 1)
                {
                    int.TryParse(args[1], out startingSecurityId);
                }
            }
            IStore store = new RedisStore();

            store.Populate(numberOfSecurities, startingSecurityId);
        }
Ejemplo n.º 27
0
        static void Main()
        {
            RedisStore instance = RedisStore.getInstance();
            IDatabase  db       = instance.RedisCacheTable;
            var        sub      = db.Multiplexer.GetSubscriber();

            sub.Subscribe(EVENTS, (channel, message) =>
            {
                string mes       = message;
                string eventName = mes.Split("=>")[0];
                if (eventName == TEXT_RANK_CALCULATED_EVENT)
                {
                    string mesVal = mes.Split("=>")[1];
                    TextStatisticConsRater textStatisticConsRater = JsonConvert.DeserializeObject <TextStatisticConsRater>(mesVal);
                    Console.WriteLine(message);
                    allTextStatistics += textStatisticConsRater.rank;
                    textNum++;

                    if (textStatisticConsRater.rank > 0.5)
                    {
                        highRankPart++;
                    }

                    avgRank = allTextStatistics / textNum;
                }

                if (eventName == PROCESSING_ACCEPTED_EVENT)
                {
                    string mesVal = mes.Split("=>")[1];
                    ProcessingAccepted processingAccepted = JsonConvert.DeserializeObject <ProcessingAccepted>(mesVal);
                    if (!processingAccepted.status)
                    {
                        countRejectRequests++;
                    }
                }

                Console.WriteLine("textNum: " + textNum + ", highRankPart: " + highRankPart +
                                  ", avgRank: " + avgRank + ", countRejectRequests" + countRejectRequests);

                db.StringSet("text_statistic", GetStringifyTextStatistic(textNum, highRankPart, avgRank, countRejectRequests));
            });

            Console.WriteLine("Observable subscribe text statistics is ready. For exit press Enter.");
            Console.ReadLine();
        }
Ejemplo n.º 28
0
        public void Redis_cache_with_value_set_remove_get()
        {
            //Arrange
            var          cache      = new RedisStore(REDIS_LOCALHOST);
            var          expiration = new TimeSpan(0, 1, 0);
            const string key        = "-Random-Key-4";

            var response = A.Fake <FakeCachedResponse>();

            //Act
            cache.Set(key, response, expiration);
            cache.Remove(key);
            bool found = cache.TryGetValue(key, out var getResponse);

            //Assert
            Assert.Null(getResponse);
            Assert.False(found);
        }
Ejemplo n.º 29
0
        public void Redis_cache_empty_set_no_timespan()
        {
            //Arrange
            var          cache      = new RedisStore(REDIS_LOCALHOST);
            var          expiration = new TimeSpan(0, 0, 0);
            const string key        = "-Random-Key-1";

            var response = A.Fake <FakeCachedResponse>();

            //Act
            cache.Set(key, response, expiration);
            bool found = cache.TryGetValue(key, out var getResponse);

            //Assert
            Assert.NotNull(cache);
            Assert.NotNull(response);
            Assert.False(found);
            Assert.Null(getResponse);
        }
Ejemplo n.º 30
0
        public virtual async Task InitializeAsync()
        {
            lock (_lock)
            {
                var builder = new ConfigurationBuilder()
                              .AddJsonFile("appsettings.json", optional: true);

                var configuration = builder.Build();

                IWebHostBuilder webHostBuild =
                    WebHost.CreateDefaultBuilder()
                    .UseStartup <TestStartUp>()
                    .UseWebRoot(Directory.GetCurrentDirectory())
                    .UseContentRoot(Directory.GetCurrentDirectory());

                var dbConnectionString = configuration.GetConnectionString("db");

                if (string.IsNullOrEmpty(dbConnectionString))
                {
                    throw new ApplicationException("Missing the connection string to database");
                }
                ;
                webHostBuild.ConfigureServices(service =>
                {
                    service.AddDbContextPool <DeviceContext>(ctxOptions => ctxOptions.UseInMemoryDatabase(dbConnectionString).EnableSensitiveDataLogging());
                    service.Configure <UserLoginOption>(options => configuration.Bind("TestUserCredential", options));
                    service.AddScoped <IHttpServiceTest, HttpServiceTest>();
                    service.AddMemoryCache();
                    service.Configure <NotificationSenderExtentionOptions>(options => configuration.Bind("NotificationHelper", options));

                    RedisConnection.InitializeConnectionString(configuration.GetSection("ConnectionStrings")["redis"]);
                    service.AddSingleton <RedisStore>();
                });
                TestServer = new TestServer(webHostBuild);
                DbContext  = TestServer.Host.Services.GetRequiredService <DeviceContext>();
                Fixture    = new Fixture();
                Fixture.Customizations.Add(new IgnoreVirtualMembers());
                memoryCache = TestServer.Host.Services.GetRequiredService <IMemoryCache>();

                _notificationSenderExtentionOptions = TestServer.Host.Services.GetRequiredService <IOptionsMonitor <NotificationSenderExtentionOptions> >();
                redisStore = TestServer.Host.Services.GetRequiredService <RedisStore>();
            }
        }
Ejemplo n.º 31
0
        static void Main()
        {
            Console.BackgroundColor = ConsoleColor.DarkGreen;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Clear();
            var client = new RedisStore("localhost:6379").CreateClient();

            client.BaseAddress = new Uri("http://localhost:1337");
            while (true)
            {
                var response = client.GetAsync("/time").Result;
                var data     = response.Content.ReadAsStringAsync().Result;
                Console.WriteLine(data);
                Console.WriteLine(response.Headers.CacheControl.ToString());
                if (Console.ReadLine() == "done")
                {
                    break;
                }
            }
        }
Ejemplo n.º 32
0
        public string Post([FromBody] string json)
        {
            var            id             = Guid.NewGuid().ToString();
            UserDataRegion userDataRegion = JsonConvert.DeserializeObject <UserDataRegion>(json);
            var            contextId      = $"RANK_{id}";
            var            instance       = RedisStore.getInstance();
            var            db             = instance.RedisCacheTable;

            var idDb     = instance.GetNumDB(userDataRegion.region);
            var regionDb = instance.RedisCache(idDb);

            db.StringSet(contextId, idDb);
            regionDb.StringSet(id, GetStringifyUserData(userDataRegion, id));

            var    pub     = db.Multiplexer.GetSubscriber();
            string message = "TextCreated=>" + id;

            pub.Publish(Events, message);

            return(id);
        }
Ejemplo n.º 33
0
 public void TestConnectivity()
 {
     var redisStore = new RedisStore();
     HttpResponseMessage responseMessage = null;
     Console.WriteLine(redisStore.TryGetValue(new CacheKey("http://google.com", new string[0]), out responseMessage));
 }