Ejemplo n.º 1
0
        Bitmap AddOrGetFromCache(Uri uri, IAssetLoader assets, Uri fallbackUri = null)
        {
            if (_cache.TryGetValue(uri, out Bitmap image))
            {
                return(image);
            }

            try
            {
                image = new Bitmap(assets.Open(uri));
                _cache.Set(uri, image);
            }
            catch (FileNotFoundException)
            {
                if (fallbackUri == null)
                {
                    return(null);
                }

                if (_cache.TryGetValue(fallbackUri, out image))
                {
                    return(image);
                }

                image = new Bitmap(assets.Open(fallbackUri));
                _cache.Set(fallbackUri, image);
            }

            return(image);
        }
Ejemplo n.º 2
0
        public override async Task <HashSet <string> > GetSetAsync(
            string key,
            Func <IEnumerable <string> > factory = null
            )
        {
            var result = IMemoryCacheService.Get <HashSet <string> >(key);

            if (result != null)
            {
                return(result);
            }

            var values = await IRedisGatewayService.GetSetAsync <string>(key);

            if (!values.HasContent())
            {
                values = factory.Invoke().ToHashSet();
            }

            await IRedisGatewayService.SetSetAsync(key, values);

            result = values.ToHashSet();

            if (_UseCacheAside)
            {
                var entryOptions = new CacheOptions <HashSet <string> >(expirationType: CacheExpirationType.NotRemoveable);
                IMemoryCacheService.Set(key, result, entryOptions);
            }

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <IDynamicCollection <T> > GetOrCreateAsync <T>(
            string listName,
            Func <IQueryable <T>, IQueryable <T> > query    = null,
            Func <IEnumerable <T>, IEnumerable <T> > sorter = null
            )
            where T : IEntity
        {
            var collectionKey = GetCollectionCacheKey(listName);

            var result =
                IMemoryCacheService.Get <IDynamicCollection <T> >(collectionKey);

            if (result != null)
            {
                return(result);
            }

            var IRepository = IContainerService.Get <IRepository <T> >();

            var idsKey    = GetIdsCacheKey(listName);
            var entityIds = await ICacheService.GetAsync <List <string> >(idsKey);

            if (entityIds == null)
            {
                var queryable = IRepository.GetQueryable();

                if (query != null)
                {
                    queryable = query(queryable);
                }

                var entities = await IDatabaseService.QueryAsync(queryable);

                await IEntityCacheService.Push(entities);

                entityIds =
                    entities
                    .Select(x => x?.Id)
                    .ToList();
            }
            else
            {
                await IRepository.GetManyByIdAsync(entityIds);
            }

            result =
                IDynamicCollectionFactory
                .Create(entityIds, sorter);

            IMemoryCacheService.Set(collectionKey, result);

            return(result);
        }
        /// <summary>
        /// Get entities from cache, if cache is empty get from database.
        /// </summary>
        /// <returns>The requested entities.</returns>
        public IEnumerable <T> GetAll()
        {
            var entities = _cacheService.Get(_key);

            if (entities == null)
            {
                CacheItemPolicy policy;
                entities = LoadEntities(out policy);
                _cacheService.Set(_key, entities, policy);
            }

            return(entities);
        }
Ejemplo n.º 5
0
        public override async Task <T> GetAsync <T>(
            string key,
            Func <T> factory         = null,
            CacheOptions <T> options = null
            )
        {
            var cacheObj =
                _UseCacheAside
                                        ? IMemoryCacheService.Get <T>(key)
                                        : default(T);

            if (cacheObj is NotFound)
            {
                return(default(T));
            }

            var result = cacheObj == null ? default(T) : cacheObj;

            if (result != null)
            {
                return(result);
            }

            result = await IRedisGatewayService.GetAsync <T>(key);

            if (result == null)
            {
                if (factory != null)
                {
                    result = factory();
                }
            }

            if (result == null)
            {
                IMemoryCacheService.Set(key, NotFound.Shared);
                return(default(T));
            }

            if (_UseCacheAside)
            {
                IMemoryCacheService.Set(key, result, options);
            }

            await IRedisGatewayService.SetAsync(key, result);

            return(result);
        }