Exemple #1
0
 public void SetItemCommon(Stash item, bool inMemOnly = false)
 {
     if (item.StashPlan == StashPlan.spSerialize)
     {
         this.SetItemSerializationOnly(item, inMemOnly);
     }
     else
     if (item.StashPlan == StashPlan.spSerializeCompress)
     {
         this.SetItemSerializeCompress(item, inMemOnly);
     }
     else
     if (item.StashPlan == StashPlan.spProtectCompress)
     {
         this.SetItemSerializeCompressEncrypt(item, inMemOnly);
     }
 }
Exemple #2
0
        protected void SetItemSerializationOnly(Stash item, bool inMemOnly = false)
        {
            string key = item.Key;

            if (!item.Serialized) // don't serialize twice.  The first time was in the Stash Object Property Setter
            {
                item.Value = JsonConvert.SerializeObject(item.Value);
            }

            item.Serialized = true;
            this.Cache.Set <Stash>(key, item, this.MemoryItemOptions);

            Stash item2 = this.Cache.Get <Stash>(key);

            if (!inMemOnly)
            {
                this.DbAddOrUpdate(item);
            }
        }
Exemple #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)
            });
        }
Exemple #4
0
        public Stash GetItemCommon(string key)
        {
            Stash item = this.Cache.Get <Stash>(key);

            if (item == null)
            {
                item = this.DbGet(key);

                if (item != null)
                {
                    Stash item_0 = this.CloneItem(item);
                    this.SetItemCommon(item_0, true);
                    item_0.Dispose();
                }
            }

            if (item == null)
            {
                return(null);
            }

            item = this.CloneItem(item);

            if (item.StashPlan == StashPlan.spSerialize)
            {
                return(this.GetItemSerializationOnly(item));
            }
            else
            if (item.StashPlan == StashPlan.spSerializeCompress)
            {
                return(this.GetItemSerializeCompress(item));
            }
            else
            if (item.StashPlan == StashPlan.spProtectCompress)
            {
                return(this.GetItemSerializeCompressEncrypt(item));
            }

            return(null);
        }
Exemple #5
0
#pragma warning disable SA1600 // Elements should be documented
        public dynamic this[string key]
#pragma warning restore SA1600 // Elements should be documented
        {
            get
            {
                try
                {
                    dynamic item = this.Stasher[key].Object;
                    if (item == null)
                    {
                        return(null);
                    }
                    else
                    {
                        return(item);
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Error: " + e.Message);
                    return(null);
                }
            }

            set
            {
                using (

                    Stash stash = new Stash()
                {
                    Key = key,
                    StashPlan = this.Plan,
                    Object = value,
                })
                {
                    this.Stasher[key] = stash;
                }
            }
        }
Exemple #6
0
        public Stash this[string key]
        {
            get
            {
                try
                {
                    return(this.GetItemCommon(key));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return(null);
                }
            }

            set
            {
                Stash clone = this.CloneItem(value);
                this.SetItemCommon(clone);

                // value.Dispose();
            }
        }
Exemple #7
0
        protected void SetItemSerializeCompressEncrypt(Stash item, bool inMemOnly = false)
        {
            string key = item.Key;

#pragma warning disable SA1108    // Block statements should not contain embedded comments
            if (!item.Serialized) // don't serialize twice.  The first time was in the Stash Object Property Setter
#pragma warning restore SA1108    // Block statements should not contain embedded comments
            {
                item.Value = JsonConvert.SerializeObject(item.Value);
            }

            string str64        = EncodeToBase64(item.Value);
            byte[] arCompressed = this.Compress(this.GetBytesFromBase64(str64));
            byte[] arProtected  = this.Protect(arCompressed);
            item.Value = Convert.ToBase64String(arProtected);

            item.Serialized = true;
            this.Cache.Set <Stash>(key, item, this.MemoryItemOptions);
            if (!inMemOnly)
            {
                this.DbAddOrUpdate(item);
            }
        }
Exemple #8
0
        protected void SetItemSerializeCompress(Stash item, bool inMemOnly = false)
        {
            string key = item.Key;

            if (!item.Serialized) // don't serialize twice.  The first time was in the Stash Object Property Setter
            {
                item.Value = JsonConvert.SerializeObject(item.Value);
            }

            item.Value = EncodeToBase64(item.Value);
            byte[] itembytes = Convert.FromBase64String(item.Value);

            byte[] arCompressed = this.Compress(itembytes);
            item.Value      = Convert.ToBase64String(arCompressed);
            item.Serialized = true;
            this.Cache.Set <Stash>(key, item, this.MemoryItemOptions);
            Stash item2 = this.Cache.Get <Stash>(key);

            if (!inMemOnly)
            {
                this.DbAddOrUpdate(item);
            }
        }
Exemple #9
0
 public bool DbAddOrUpdate(Stash item)
 {
     this.DB.Add <Stash>(item.Key, item, TimeSpan.FromDays(365), Hash(item.Key));
     return(true);
 }