Ejemplo n.º 1
0
 public virtual void Set(object id, TEntity entity)
 {
     entityCache[id] = new CacheBox <TEntity> {
         Value = entity
     };
     secondLevelCache.Set(id, entity);
 }
Ejemplo n.º 2
0
        public virtual TEntity Get(object id, Func <object, TEntity> factory = null)
        {
            CacheBox <TEntity> box;

            if (entityCache.TryGetValue(id, out box))
            {
                return(box.Value);
            }

            var entity = secondLevelCache.Get(id);

            if (entity != null)
            {
                entityCache[id] = new CacheBox <TEntity> {
                    Value = entity
                };
                hydrate(entity);
                return(entity);
            }

            entity = factory(id);
            Set(id, entity);

            return(entity);
        }
Ejemplo n.º 3
0
        public virtual IEnumerable <TEntity> Query(object query, Func <IEnumerable <Tuple <object, TEntity> > > resultsFactory = null, Func <object, TEntity> entityFactory = null)
        {
            CacheBox <IEnumerable <object> > box;

            if (queryCache.TryGetValue(query, out box))
            {
                return(box.Value.Select(id => Get(id, entityFactory)));
            }

            var cachedIds = secondLevelCache.GetQuery(query);

            if (cachedIds != null)
            {
                return(cachedIds.Select(id => Get(id, entityFactory)));
            }

            if (resultsFactory == null)
            {
                return(null);
            }
            if (entityFactory == null)
            {
                return(null);
            }

            var results  = resultsFactory();
            var ids      = new List <object>();
            var entities = new List <TEntity>();

            foreach (var result in results)
            {
                ids.Add(result.Item1);
                if (!Exists(result.Item1))
                {
                    Set(result.Item1, result.Item2);
                }
                entities.Add(result.Item2);
            }
            box = new CacheBox <IEnumerable <object> > {
                Value = ids
            };
            queryCache[query] = box;
            secondLevelCache.SetQuery(query, ids);
            return(entities);
        }
Ejemplo n.º 4
0
 public virtual void SetQuery(object query, IEnumerable <object> ids)
 {
     queryCache[query] = new CacheBox <IEnumerable <object> > {
         Value = ids
     };
 }
Ejemplo n.º 5
0
 public virtual void Set(object id, TEntity entity)
 {
     entityCache[id] = new CacheBox <TEntity> {
         Value = dehydrate(entity)
     };
 }