private ICacheItemExpiration[] GetCacheExpirations()
 {
     ICacheItemExpiration[] cachingExpirations = new ICacheItemExpiration[2];
     cachingExpirations[0] = new AbsoluteTime(new TimeSpan(0, 0, ConvertExpirationTimeToSeconds(absoluteExpiration)));
     cachingExpirations[1] = new SlidingTime(new TimeSpan(0, 0, ConvertExpirationTimeToSeconds(slidingExpiration)));
     return(cachingExpirations);
 }
Esempio n. 2
0
        public static EmployeesDataSet GetContactDetails()
        {
            EmployeesDataSet dsEmployees = null;

            // Attempt to retrieve from cache
            ICacheManager cache = EnterpriseLibraryContainer.Current.GetInstance <ICacheManager>();

            dsEmployees = (EmployeesDataSet)cache[CACHE_KEY];

            // Retrieve from dataProvider if not in Cache and Online
            if (dsEmployees == null && ConnectionManager.IsOnline)
            {
                EmployeeDataProvider dataProvider = new EmployeeDataProvider();
                dsEmployees = dataProvider.GetEmployees();

                // Expire in 2 days
                AbsoluteTime expiry = new AbsoluteTime(new TimeSpan(2, 0, 0, 0));
                cache.Add(
                    CACHE_KEY,
                    dsEmployees,
                    CacheItemPriority.High,
                    null,
                    new ICacheItemExpiration[] { expiry });
            }

            return(dsEmployees);
        }
Esempio n. 3
0
        //获取设置的异常过期策略
        private void GetICacheItemExpiration(string interval, string assemblyName, string className, ref ICacheItemExpiration[] ie)
        {
            switch (className)
            {
            case "Microsoft.ApplicationBlocks.Cache.ExpirationsImplementations.SlidingTime":
                double dinterval = double.Parse(interval);
                ie    = new SlidingTime[1];
                ie[0] = new SlidingTime(TimeSpan.FromSeconds(dinterval));
                break;

            case "Microsoft.ApplicationBlocks.Cache.ExpirationsImplementations.AbsoluteTime":
                double          secondsAfter = double.Parse(interval);
                System.DateTime dt           = System.DateTime.Now.AddSeconds(secondsAfter);
                ie    = new AbsoluteTime[1];
                ie[0] = new AbsoluteTime(dt);
                break;

            case "Microsoft.ApplicationBlocks.Cache.ExpirationsImplementations.FileDependency":
                ie    = new FileDependency[1];
                ie[0] = new FileDependency(interval);
                break;

            case "Microsoft.ApplicationBlocks.Cache.ExpirationsImplementations.ExtendedFormatTime":
                ie    = new ExtendedFormatTime[1];
                ie[0] = new ExtendedFormatTime(interval);
                break;

            default:
                ICacheItemExpiration ieTemp = Assembly.Load(assemblyName).CreateInstance(className) as ICacheItemExpiration;
                ie    = Array.CreateInstance(ieTemp.GetType(), 1) as ICacheItemExpiration[];
                ie[0] = ieTemp;
                break;
            }
        }
Esempio n. 4
0
 public void WillExpireAfterOneSecondFromNow()
 {
     AbsoluteTime expiration = new AbsoluteTime(TimeSpan.FromSeconds(0.2));
     Assert.IsFalse(expiration.HasExpired(), "Should not be expired immediately after creation");
     Thread.Sleep(400);
     Assert.IsTrue(expiration.HasExpired(), "Should have expired by now");
 }
        public void ItemsGetRemovedFromCacheAfterExpiration()
        {
            Assert.IsNull(cache.GetData("key"));

            DataBackingStore backingStore = CreateDataBackingStore();

            Assert.AreEqual(0, backingStore.Load().Count);

            AbsoluteTime threeSecondExpiration = new AbsoluteTime(DateTime.Now + TimeSpan.FromSeconds(3.0));

            lock (callbackLock)
            {
                cache.Add("key", "value", CacheItemPriority.NotRemovable,
                          new RefreshAction(),
                          threeSecondExpiration);

                Assert.IsNotNull(cache.GetData("key"));

                Hashtable oneEntryHashTable = backingStore.Load();
                Assert.AreEqual(1, oneEntryHashTable.Count);

                Monitor.Wait(callbackLock, 15000);

                Assert.IsTrue(wasCalledBack);
            }

            object removedItem = cache.GetData("key");

            Assert.IsNull(removedItem);

            Hashtable emptyHashTable = backingStore.Load();

            Assert.AreEqual(0, emptyHashTable.Count);
        }
Esempio n. 6
0
        /// <summary>
        /// 添加缓存 过期时间
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="timespan">过期时间默认2分钟 固定时间过期</param>
        public static void Add2(string key, object value, TimeSpan timespan)
        {
            timespan = timespan == null ? new TimeSpan(0, 2, 0) : timespan;
            AbsoluteTime expireTime = new AbsoluteTime(timespan);

            cache.Add(key, value, CacheItemPriority.Normal, null, expireTime);
        }
Esempio n. 7
0
        /// <summary>
        /// Insert value into the cache using
        /// appropriate name/value pairs
        /// </summary>
        /// <typeparam name="T">Type of cached item</typeparam>
        /// <param name="o">Item to be cached</param>
        /// <param name="key">Name of item</param>
        public static void Add <T>(string key, T o)
        {
            //Adding  cache expiration policy
            int          cacheExpireTimeOut = Convert.ToInt32(ConfigurationManager.AppSettings["CacheExpireTimeOut"]);
            AbsoluteTime absolute           = new AbsoluteTime(TimeSpan.FromMinutes(cacheExpireTimeOut));

            objCacheManager.Add(key, o, CacheItemPriority.Normal, null, absolute);
        }
        public void WillExpireAfterOneSecondFromNow()
        {
            AbsoluteTime expiration = new AbsoluteTime(TimeSpan.FromSeconds(0.2));

            Assert.IsFalse(expiration.HasExpired(), "Should not be expired immediately after creation");
            Thread.Sleep(400);
            Assert.IsTrue(expiration.HasExpired(), "Should have expired by now");
        }
Esempio n. 9
0
        public static void Set <T>(string key, object value,
                                   int minuteAbsoluteExpiration,
                                   Action <T> functionCacheChangeStatus, T paraAction)
        {
            AbsoluteTime _AbsoulteTime = new AbsoluteTime(TimeSpan.FromMinutes(minuteAbsoluteExpiration));

            cacheManager.Add(key, value, CacheItemPriority.Normal,
                             new ItemCacheRefreshAction <T>(functionCacheChangeStatus, paraAction), _AbsoulteTime);
        }
Esempio n. 10
0
        private ICacheItemExpiration[] GetCacheExpirations()
        {
            CachingStoreProviderData cacheStorageProviderData = GetCacheStorageProviderData();

            ICacheItemExpiration[] cachingExpirations = new ICacheItemExpiration[2];
            cachingExpirations[0] = new AbsoluteTime(new TimeSpan(0, 0, ConvertExpirationTimeToSeconds(cacheStorageProviderData.AbsoluteExpiration)));
            cachingExpirations[1] = new SlidingTime(new TimeSpan(0, 0, ConvertExpirationTimeToSeconds(cacheStorageProviderData.SlidingExpiration)));
            return(cachingExpirations);
        }
Esempio n. 11
0
        public static void Set(string key, object value, int minuteAbsoluteExpiration, string cachePath, string fileName)
        {
            //Creating Absolute Time Expiration
            AbsoluteTime _AbsoulteTime = new AbsoluteTime(TimeSpan.FromMinutes(minuteAbsoluteExpiration));
            //Creating FileDependecy Object
            FileDependency _objFileDependency = new FileDependency(fileName); //"MyFile.XML"

            // Using ICacheItemExpiration To Set multiple Cache Expiration policy
            cacheManager.Add(key, value, CacheItemPriority.Normal, null, new ICacheItemExpiration[] { _AbsoulteTime, _objFileDependency });
        }
Esempio n. 12
0
        public void ClassCanSerializeCorrectly()
        {
            AbsoluteTime absoluteTime = new AbsoluteTime(DateTime.Now.AddDays(2));

            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            formatter.Serialize(stream, absoluteTime);
            stream.Position = 0;
            AbsoluteTime absoluteTime2 = (AbsoluteTime)formatter.Deserialize(stream);

            Assert.AreEqual(absoluteTime.AbsoluteExpirationTime, absoluteTime2.AbsoluteExpirationTime);
        }
        public void ClassCanSerializeCorrectly()
        {
            AbsoluteTime absoluteTime = new AbsoluteTime(DateTime.Now.AddDays(2));

            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream    = new MemoryStream();

            formatter.Serialize(stream, absoluteTime);
            stream.Position = 0;
            AbsoluteTime absoluteTime2 = (AbsoluteTime)formatter.Deserialize(stream);

            Assert.AreEqual(absoluteTime.AbsoluteExpirationTime, absoluteTime2.AbsoluteExpirationTime);
        }
Esempio n. 14
0
 /// <summary>
 /// 将Cache的数据加到缓存manager中
 /// </summary>
 /// <param name="cacheData"></param>
 /// <param name="entityCfg"></param>
 private void addCacheDataToContainer(string key, CacheData cacheData, EntityCfg entityCfg)
 {
     if (entityCfg != null)
     {
         List <ICacheItemExpiration> cacheItemExpiration = new List <ICacheItemExpiration>();
         foreach (var cacheExpireCfg in entityCfg.Expirations)
         {
             if (cacheExpireCfg.ExpirationType == CacheExpirationType.AbsoluteTime)
             {
                 TimeSpan ts = TimeSpan.FromMilliseconds(cacheExpireCfg.ExpireTime);
                 if (ts.TotalMilliseconds > 0)
                 {
                     AbsoluteTime absoluteTime = new AbsoluteTime(ts);
                     cacheItemExpiration.Add(absoluteTime);
                 }
             }
             else if (cacheExpireCfg.ExpirationType == CacheExpirationType.SlidingTime)
             {
                 TimeSpan ts = TimeSpan.FromMilliseconds(cacheExpireCfg.ExpireTime);
                 if (ts.TotalMilliseconds > 0)
                 {
                     SlidingTime slidingTime = new SlidingTime(ts);
                     cacheItemExpiration.Add(slidingTime);
                 }
             }
             else if (cacheExpireCfg.ExpirationType == CacheExpirationType.FileDependency)
             {
                 if (System.IO.File.Exists(cacheExpireCfg.FilePath))
                 {
                     FileDependency fileDependency = new FileDependency(cacheExpireCfg.FilePath);
                     cacheItemExpiration.Add(fileDependency);
                 }
             }
         }
         if (entityCfg.IsCacheItemRefreshed)
         {
             _EntitySetCache.Add(key, cacheData, entityCfg.CacheItemPriority, new CacheItemRefreshAction(entityCfg, key), cacheItemExpiration.ToArray());
         }
         else
         {
             _EntitySetCache.Add(key, cacheData, entityCfg.CacheItemPriority, null, cacheItemExpiration.ToArray());
         }
     }
     else
     {
         _EntitySetCache.Add(key, cacheData);
     }
 }
Esempio n. 15
0
        /// <summary>
        /// 添加缓存
        /// by Hades.Gao
        /// </summary>
        /// <param name="cKey">缓存关键字</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="_CacheTimeOut">过期时间,以秒作单位</param>
        public void AddCache(string cKey, object obj, int _CacheTimeOut)
        {
            try
            {
                TimeSpan     refreshTime = new TimeSpan(0, 0, _CacheTimeOut);
                AbsoluteTime expireTime  = new AbsoluteTime(refreshTime);

                if (!Object.Equals(_DataCacheManager, null))
                {
                    _DataCacheManager.Add(cKey, obj, CacheItemPriority.Normal, null, expireTime);
                }
            }
            catch (Exception ex)
            {
                this.LogInfo("添加缓存时出错:" + ex.Message + "\r\nTargetSite:" + ex.TargetSite + "\r\nKey:" + cKey + "\r\nObjectType:" + obj.GetType() + "\r\nObject:" + obj.ToString() + "\r\nTimeOut:" + _CacheTimeOut.ToString());
            }
        }
        public List<Product> GetProductData(out string cacheStatus)
        {
            cacheProductData = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>();
            listProducts = (List<Product>)cacheProductData["ProductDataCache"];
            cacheStatus = "Employee Information is Retrived from Cache";
            LetsShopImplementation ls = new LetsShopImplementation();
            CacheFlag = true;
            if (listProducts == null)
            {
              //Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnection");
              //listProducts = 

                LetsShopImplementation ls = new LetsShopImplementation();
                listProducts = ls.GetProducts();
                AbsoluteTime CacheExpirationTime = new AbsoluteTime(new TimeSpan(1, 0, 0));
                cacheProductData.Add("ProductDataCache", listProducts, CacheItemPriority.High, null, new ICacheItemExpiration[] { CacheExpirationTime });
                cacheStatus = "Product Info is Added in cache";
                CacheFlag = false;
            }
            return listProducts;

            }
Esempio n. 17
0
        private void AddToCache(string key, object valueToCache)
        {
            if (key == null)
            {
                //When the method uses input that is not serializable
                //we cannot create a cache key and can therefore not
                //cache the data.
                return;
            }

            if (expirationTime.Equals(TimeSpan.Zero))
            {
                cacheManager.Add(key, valueToCache);
            }
            else
            {
                var expiry = new AbsoluteTime(expirationTime);
                cacheManager.Add(key,
                                 valueToCache,
                                 CacheItemPriority.Normal,
                                 null,
                                 new ICacheItemExpiration[] { expiry });
            }
        }
Esempio n. 18
0
 public void ThrowsExceptionIfTimeInPast()
 {
     AbsoluteTime expirationInThePast = new AbsoluteTime(TimeSpan.FromSeconds(-1.0));
 }
Esempio n. 19
0
        /// <summary>TBD</summary>
        private static void PopulateApplicationCache()
        {
            ICacheManager appCache = CacheFactory.GetCacheManager();
            //Common Expiration time, used by cached data which is built from tables that refresh as part of the SQL Server scheduled job
            AbsoluteTime expiry = new AbsoluteTime(new TimeSpan(0, 3, 30, 0));

            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.ADDepartments)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.ADDepartments), GetADDepartments(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.ADOffices)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.ADOffices), GetADOffices(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.MarketDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.MarketDataSetType), GetMarkets(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.MediaFormDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.MediaFormDataSetType), GetMediaForms(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.NewMediaFormDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.NewMediaFormDataSetType), GetNewMediaForms(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.NewMediaTypeDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.NewMediaTypeDataSetType), GetNewMediaTypes(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.MediaTypeDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.MediaTypeDataSetType), GetMediaTypes(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.AEDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.AEDataSetType), GetAEs(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.ConsolidatedAdvertiserType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.ConsolidatedAdvertiserType), GetConsolidatedAdvertisers(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.AgencyDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.AgencyDataSetType), GetAgencies(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.AdvertiserDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.AdvertiserDataSetType), GetAdvertisers(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.AppDefaultDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.AppDefaultDataSetType), GetAppDefaults(), CacheItemPriority.High, null, new ICacheItemExpiration[] { new AbsoluteTime(new TimeSpan(0, 12, 0, 0)) });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.ContractLineYearsDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.ContractLineYearsDataSetType), GetContractLineItemYears(), CacheItemPriority.High, null, new ICacheItemExpiration[] { new AbsoluteTime(new TimeSpan(0, 12, 0, 0)) });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.ProfitCenterDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.ProfitCenterDataSetType), GetProfitCenters(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.ParentProductClassDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.ParentProductClassDataSetType), GetParentProductClasses(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.ProductClassDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.ProductClassDataSetType), GetProductClasses(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.ProductClassDLDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.ProductClassDLDataSetType), GetProductClassesDL(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.StationDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.StationDataSetType), GetStations(), CacheItemPriority.High, null, new ICacheItemExpiration[] { expiry });
            }
            if (!appCache.Contains(Enum.GetName(typeof(DataSetType), DataSetType.UploadedByDataSetType)))
            {
                appCache.Add(Enum.GetName(typeof(DataSetType), DataSetType.UploadedByDataSetType), GetUploadedBy(), CacheItemPriority.High, null, new ICacheItemExpiration[] { new AbsoluteTime(new TimeSpan(0, 0, 30, 0)) });
            }
        }
Esempio n. 20
0
        public static void Set(string key, object value, int minuteAbsoluteExpiration)
        {
            AbsoluteTime _AbsoulteTime = new AbsoluteTime(TimeSpan.FromMinutes(minuteAbsoluteExpiration));

            cacheManager.Add(key, value, CacheItemPriority.Normal, null, _AbsoulteTime);
        }
Esempio n. 21
0
 public void ThrowsExceptionIfTimeEqualToNow()
 {
     AbsoluteTime expirationRightNow = new AbsoluteTime(TimeSpan.FromSeconds(0.0));
 }
Esempio n. 22
0
        /// <summary>
        /// Insert value into the cache using
        /// appropriate name/value pairs
        /// </summary>
        /// <typeparam name="T">Type of cached item</typeparam>
        /// <param name="o">Item to be cached</param>
        /// <param name="key">Name of item</param>
        public static void Add <T>(string key, T o, double cacheExpireTimeOutinMinutes)
        {
            AbsoluteTime absolute = new AbsoluteTime(TimeSpan.FromMinutes(cacheExpireTimeOutinMinutes));

            objCacheManager.Add(key, o, CacheItemPriority.Normal, null, absolute);
        }
Esempio n. 23
0
 public void ThrowsExceptionIfTimeInPast()
 {
     AbsoluteTime expirationInThePast = new AbsoluteTime(TimeSpan.FromSeconds(-1.0));
 }
Esempio n. 24
0
 public void ThrowsExceptionIfTimeEqualToNow()
 {
     AbsoluteTime expirationRightNow = new AbsoluteTime(TimeSpan.FromSeconds(0.0));
 }
Esempio n. 25
0
 public ReplayBlock GetBlockData(AbsoluteTime time)
 {
     return(blocks.FirstOrDefault(item => item.Value.BaseRange.Contains(time)).Value);
 }
Esempio n. 26
0
        public void Add(string key, object value, DateTime absolute)
        {
            ICacheItemExpiration expiration = new AbsoluteTime(absolute);

            _cache.Add(key, value, CacheItemPriority.Normal, null, expiration);
        }
Esempio n. 27
0
 /// <inheritdoc />
 public override string ToString()
 {
     return(Type == SmppDateTimeType.Relative ? RelativeTime.ToString() : AbsoluteTime.ToString());
 }