Пример #1
0
        private bool Populate(VirtualCache cache, bool slidingExpiration, int start)
        {
            var target = cache;

            _logger.Information("{0}: Start of Populating target cache {1}.", DateTime.Now, target.Name);
            try
            {
                // using a batch allows more efficient use of SOP data store so it can do bulk insert.
                CacheKeyValue[] batch  = new CacheKeyValue[5000];
                var             policy = new CacheItemPolicy()
                {
                    SlidingExpiration = new TimeSpan(0, 15, 0)
                };
                for (int i = start; i < start + MaxCacheEntries; i++)
                {
                    batch[i % batch.Length] = new CacheKeyValue()
                    {
                        Key    = string.Format("Hello{0}", i),
                        Value  = string.Format("Value{0}", i),
                        Policy = policy
                    };
                    if (i % batch.Length == batch.Length - 1)
                    {
                        target.SetValues(batch);
                    }
                }
            }
            catch (Exception exc)
            {
                _logger.Fatal(exc, "{0}: Failed Populating target cache {1}.", DateTime.Now, target.Name);
                return(false);
            }
            _logger.Information("{0}: End Populating target cache {1}.", DateTime.Now, target.Name);
            return(true);
        }
Пример #2
0
        public override async Task <CacheSavedReply> Save(CacheKeyValue request, ServerCallContext context)
        {
            try
            {
                await Task.Run(() => redisCache.Set(request.Key, request.Value));

                return(new CacheSavedReply
                {
                    Message = $"{request.Value} saved in memory cache!"
                });
            }
            catch (Exception ex)
            {
                return(new CacheSavedReply
                {
                    Message = ex.Message
                });
            }
        }