Example #1
0
        public Stasher(
            string purpose,
            StashPlan plan,
            StashRepo db,
            IDataProtector dataProtector,
            IMemoryCache cache,
            MemoryCacheEntryOptions memoryItemOptions = null)
        {
            this.DB            = db;
            this.Cache         = cache;
            this.DataProtector = dataProtector;
            this.Purpose       = purpose;
            this.Plan          = plan;

            // If developer chooses to instantiate standalone Stasher then we will need to deal with
            // memItemOptions directly
            if (memoryItemOptions == null)
            {
                this.MemoryItemOptions = new MemoryCacheEntryOptions()
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1),
                    Size     = 921600, // bytes = 900 kb
                    Priority = CacheItemPriority.High,
                };
            }
            else
            {
                if (memoryItemOptions.Size == null)
                {
                    memoryItemOptions.Size = 921600;
                }

                this.MemoryItemOptions = memoryItemOptions;
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StacheMeister"/> class.
        /// </summary>
        /// <param name="appId">appId.</param>
        /// <param name="filename">filename.</param>
        /// <param name="password">password.</param>
        /// <param name="plan">plan.</param>
        /// <param name="memCacheOptions">memCacheOptions.</param>
        /// <param name="memoryItemOptions">memoryItemOptions.</param>
        /// <param name="services">services.</param>
        public StacheMeister(
            string appId,
            string filename = null,
            string password = null,
            StashPlan plan  = StashPlan.spSerialize,
            MemoryCacheOptions memCacheOptions        = null,
            MemoryCacheEntryOptions memoryItemOptions = null,
            ServiceCollection services = null)
        {
            this.Plan  = plan;
            this.AppId = appId;

            this.DB = new StashRepo(appId, filename, password);
            ServiceCollection svcs = services ?? new ServiceCollection();

            this.Services = svcs;
            svcs.AddDataProtection();
            svcs.AddMemoryCache();
            this.ServiceProvider = svcs.BuildServiceProvider();
            if (memCacheOptions == null)
            {
                this.Cache = new MemoryCache(new MemoryCacheOptions()
                {
                    SizeLimit               = 5242880, // bytes = 5 megs
                    CompactionPercentage    = .50,
                    ExpirationScanFrequency = TimeSpan.FromMinutes(1),
                });
            }
            else
            {
                this.Cache = new MemoryCache(memCacheOptions);
            }

            if (memoryItemOptions == null)
            {
                this.MemoryItemOptions = new MemoryCacheEntryOptions()
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1),
                    Size     = 921600, // bytes = 900 kb
                    Priority = CacheItemPriority.High,
                };
            }
            else
            {
                if (memoryItemOptions.Size == null)
                {
                    memoryItemOptions.Size = 921600;
                }

                this.MemoryItemOptions = memoryItemOptions;
            }

            this.DataProtectionProvider = this.ServiceProvider.GetService <IDataProtectionProvider>();
            this.DataProtector          = this.DataProtectionProvider.CreateProtector(appId);
            this.DefaultStashers();
        }
Example #3
0
        protected Stash CloneItem(Stash stash)
        {
            StashPlan itemPlan = this.GetPlanFromValue(stash.Plan);

            if (this.Plan != itemPlan)
            {
                throw new Exception("Cache Item Plan does not match container's Plan.");
            }

            return(new Stash()
            {
                ExpirationDate = stash.ExpirationDate,
                Hash = stash.Hash,
                Key = stash.Key,
                Serialized = stash.Serialized,
                Size = stash.Size,
                Value = stash.Value,
                StoredType = stash.StoredType,
                StashPlan = itemPlan, // GetPlanFromValue(stash.Plan)
            });
        }
Example #4
0
 public Stasher MakeStasher(string purpose, StashPlan plan = StashPlan.spSerialize)
 {
     return(new Stasher(purpose, plan, this.DB, this.DataProtector, this.Cache));
 }