Ejemplo n.º 1
0
        public static IDictionary <string, T> GetFromCache <T>(this ICacheClient cacheClient,
                                                               IEnumerable <string> cacheKeys,
                                                               string pattern,
                                                               Func <object, T> factoryFn)
        {
            using (IRedisClient redisClient = (cacheClient as  IRedisClientsManager).GetClient()){
                List <string> res = redisClient.SearchKeys(pattern);

                if (cacheKeys != null && cacheKeys.ToList().Count > 0)
                {
                    var s = (from r in res
                             from c in cacheKeys
                             where r == c
                             select r).ToList();

                    var cache = (s != null && s.Count > 0)? cacheClient.GetAll <T>(s):  new Dictionary <string, T>();

                    foreach (string key in cacheKeys)
                    {
                        if (!s.Contains(key))
                        {
                            T t = factoryFn(UrnId.GetStringId(key));
                            if (t != null)
                            {
                                cacheClient.Set <T>(key, t);
                                cache.Add(key, t);
                            }
                        }
                    }
                    return(cache);
                }
                else
                {
                    return((res != null && res.Count > 0)? cacheClient.GetAll <T>(res): new Dictionary <string, T>());;
                }
            }
        }
Ejemplo n.º 2
0
        public List <TEntity> GetByIds <TEntity>(ICollection entityIds)
            where TEntity : class, new()
        {
            if (entityIds.Count == 0)
            {
                return(new List <TEntity>());
            }

            var cacheKeys        = entityIds.ConvertAll(x => x.CreateUrn());
            var cacheEntitiesMap = this.CacheClient.GetAll <TEntity>(cacheKeys);

            if (cacheEntitiesMap.Count < entityIds.Count)
            {
                var entityIdType = entityIds.First().GetType();

                var entityIdsNotInCache = cacheKeys
                                          .Where(x => !cacheEntitiesMap.ContainsKey(x))
                                          .ConvertAll(x =>
                                                      TypeSerializer.DeserializeFromString(UrnId.GetStringId(x), entityIdType));

                using (var db = GetBasicPersistenceProvider())
                {
                    var cacheEntities = db.GetByIds <TEntity>(entityIdsNotInCache);

                    foreach (var cacheEntity in cacheEntities)
                    {
                        var cacheKey = cacheEntity.CreateUrn();
                        this.CacheClient.Set(cacheKey, cacheEntity);
                        cacheEntitiesMap[cacheKey] = cacheEntity;
                    }
                }
            }

            return(cacheEntitiesMap.Values.ToList());
        }