Example #1
0
        /// <summary>
        /// Inserts instance of an entity in the cache. Any exisiting entity will be overwritten in the cache.
        /// Entity should be a part of the database context or else the method will throw an exception.
        /// </summary>
        /// <param name="entity">Instance of the entity to be inserted.</param>
        /// <param name="cacheKey">cache key that was used to insert the entity.</param>
        /// <param name="options">Caching options to be used while storing the entity. Note that some of the options
        /// might be overridden such as StoreAs option will always be <see cref="StoreAs.SeperateEntities"/>.</param>
        public void Insert(object entity, out string cacheKey, CachingOptions options)
        {
            Logger.Log(
                "Inserting entity '" + entity + "' with options " + options.ToLog() + "",
                Microsoft.Extensions.Logging.LogLevel.Trace
                );

            if (IsValidEntity(entity))
            {
                NCacheWrapper nCacheWrapper = QueryCacheManager.Cache;

                // Generate key using the key generator from NCacheWrapper
                cacheKey = nCacheWrapper.DefaultKeyGen.GetKey(CurrentContext, entity);

                //
                // Items are stored as separate entities in this API because only separate APIs can make it
                // to this section of code. List or other similar data structures will fail on IsValidEntity
                // so only individual entities will make it to here.
                //
                nCacheWrapper.Set(cacheKey, entity, options, null, StoreAs.SeperateEntities);

                return;
            }
            else
            {
                throw new Exception("Entity type and context do not match");
            }
        }
Example #2
0
 internal static TItem Set <TItem>(this NCacheWrapper cache, object key, TItem value, CachingOptions options, Alachisoft.NCache.Runtime.Dependencies.CacheDependency dbDependency, StoreAs storingAs)
 {
     Logger.Log(
         "Setting item '" + value + "' against key '" + key + "' with DbDependency '" + dbDependency + "'.",
         Microsoft.Extensions.Logging.LogLevel.Trace
         );
     Alachisoft.NCache.Web.Caching.CacheItem cacheItem = new Alachisoft.NCache.Web.Caching.CacheItem(value);
     CachingOptionsUtil.CopyMetadata(ref cacheItem, options, dbDependency);
     cache.Insert(key, cacheItem);
     return(value);
 }
Example #3
0
        public string GetEntityCacheKey(object entity)
        {
            NCacheWrapper nCacheW = QueryCacheManager.Cache as NCacheWrapper;

            if (nCacheW != null)
            {
                StringBuilder keyBuilder = new StringBuilder();
                keyBuilder.Append(nCacheW.DefaultKeyGen.GetKey(_currentContext, entity));
                return(keyBuilder.ToString());
            }
            // Handle other cases if needed
            throw new Exception("Cache is not NCache.");
        }
Example #4
0
        internal static List <TItem> Set <TItem>(this NCacheWrapper cache, object key, Dictionary <string, TItem> value, CachingOptions options, Alachisoft.NCache.Runtime.Dependencies.CacheDependency dbDependency, StoreAs storingAs)
        {
            Logger.Log(
                "About to set values with options " + options.ToLog() + ", DbDependency '" + dbDependency + "' and StoringAs '" + storingAs + "'.",
                Microsoft.Extensions.Logging.LogLevel.Trace
                );

            // Add entities if stroing as seperateEntities
            if (storingAs == StoreAs.SeperateEntities)
            {
                Logger.Log("Values are about to be set as separate entities.", Microsoft.Extensions.Logging.LogLevel.Trace);
                cache.Set(value.Keys.ToArray(), value.Values.ToArray(), options, dbDependency, storingAs);
            }
            // from here onwards is the enumerator logic and now it is being done in "else" after we have moved to tags based result set regeneration
            else
            {
                Logger.Log("Values are about to be set as collection.", Microsoft.Extensions.Logging.LogLevel.Trace);

                // Add query enumerator
                CacheEntry entry = cache.CreateEntry(key);

                // Setting options
                if (options != null)
                {
                    entry.SetOptions(options);
                }

                // Setting Value
                if (storingAs == StoreAs.Collection)
                {
                    entry.Value = value.Values.ToList();
                }

                // Mind that this is not the user specified option but the end storing methodology
                entry.StoredAs = storingAs;

                // Set dependencies in the entry
                var aggregateDependency = new AggregateCacheDependency();
                if (dbDependency != null)
                {
                    aggregateDependency.Add(dbDependency);
                }

                entry.Dependencies = aggregateDependency;

                cache.Set(key, entry, options, dbDependency, storingAs);
            }
            return(value.Values.ToList());
        }
Example #5
0
        /// <summary>
        /// Removes the entity from the cache. Doesnt throw any exception if the entity does not exist.
        /// However entity should be a part of the database context or else the method will throw an exception.
        /// </summary>
        /// <param name="entity">Entity that will be used to generate the Key for the cache item.</param>
        public void Remove(object entity)
        {
            Logger.Log("Removing entity '" + entity + "'", Microsoft.Extensions.Logging.LogLevel.Trace);

            if (IsValidEntity(entity))
            {
                NCacheWrapper nCacheWrapper = QueryCacheManager.Cache;
                nCacheWrapper.Remove(
                    nCacheWrapper.DefaultKeyGen.GetKey(CurrentContext, entity)
                    );
            }
            else
            {
                throw new Exception("Entity type and context do not match");
            }
        }
Example #6
0
        internal static TItem[] Set <TItem>(this NCacheWrapper cache, object[] keys, TItem[] values, CachingOptions options, Alachisoft.NCache.Runtime.Dependencies.CacheDependency dbDependency, StoreAs storingAs)
        {
            Logger.Log(
                "Setting items in bulk against respective keys with DbDependency '" + dbDependency + "'.",
                Microsoft.Extensions.Logging.LogLevel.Trace
                );

            Alachisoft.NCache.Web.Caching.CacheItem[] cacheItems = new Alachisoft.NCache.Web.Caching.CacheItem[values.Count()];
            for (int i = 0; i < values.Count(); i++)
            {
                cacheItems[i] = new Alachisoft.NCache.Web.Caching.CacheItem(values[i]);
                CachingOptionsUtil.CopyMetadata(ref cacheItems[i], options, dbDependency);
            }
            if (keys.Length > 0)
            {
                cache.InsertBulk(keys, cacheItems);
            }
            return(values);
        }
Example #7
0
        internal static TItem SetAsCacheEntry <TItem>(this NCacheWrapper cache, object key, TItem value, CachingOptions options, CacheDependency dbDependency)
        {
            Logger.Log(
                "Setting CacheEntry '" + value + "' against key '" + key + "' with DbDependency '" + dbDependency + "'",
                Microsoft.Extensions.Logging.LogLevel.Trace
                );

            CacheEntry entry = cache.CreateEntry(key);

            if (options != null)
            {
                entry.SetOptions(options);
            }
            if (dbDependency != null)
            {
                entry.Dependencies = dbDependency;
            }
            entry.Value = value;

            cache.Set(key, entry, options, dbDependency, options.StoreAs);

            return(value);
        }
Example #8
0
 internal static TItem SetAsCacheEntry <TItem>(this NCacheWrapper cache, object key, TItem value, CachingOptions options)
 {
     return(SetAsCacheEntry(cache, key, value, options, null));
 }