Personal project framework construction and project framework construction

Source: Internet
Author: User
Tags nopcommerce

Personal project framework construction and project framework construction

1. cache Interface

using System;using System.Collections.Generic;using System.Runtime.Caching;using System.Text.RegularExpressions;namespace EnterpriseFrame.Core.Caching{    /// <summary>    /// Represents a manager for caching between HTTP requests (long term caching)    /// </summary>    public partial class MemoryCacheManager : ICacheManager    {        protected ObjectCache Cache        {            get            {                return MemoryCache.Default;            }        }                /// <summary>        /// Gets or sets the value associated with the specified key.        /// </summary>        /// <typeparam name="T">Type</typeparam>        /// <param name="key">The key of the value to get.</param>        /// <returns>The value associated with the specified key.</returns>        public virtual T Get<T>(string key)        {            return (T)Cache[key];        }        /// <summary>        /// Adds the specified key and object to the cache.        /// </summary>        /// <param name="key">key</param>        /// <param name="data">Data</param>        /// <param name="cacheTime">Cache time</param>        public virtual void Set(string key, object data, int cacheTime)        {            if (data == null)                return;            var policy = new CacheItemPolicy();            policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);            Cache.Add(new CacheItem(key, data), policy);        }        /// <summary>        /// Gets a value indicating whether the value associated with the specified key is cached        /// </summary>        /// <param name="key">key</param>        /// <returns>Result</returns>        public virtual bool IsSet(string key)        {            return (Cache.Contains(key));        }        /// <summary>        /// Removes the value with the specified key from the cache        /// </summary>        /// <param name="key">/key</param>        public virtual void Remove(string key)        {            Cache.Remove(key);        }        /// <summary>        /// Removes items by pattern        /// </summary>        /// <param name="pattern">pattern</param>        public virtual void RemoveByPattern(string pattern)        {            var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);            var keysToRemove = new List<String>();            foreach (var item in Cache)                if (regex.IsMatch(item.Key))                    keysToRemove.Add(item.Key);            foreach (string key in keysToRemove)            {                Remove(key);            }        }        /// <summary>        /// Clear all cache data        /// </summary>        public virtual void Clear()        {            foreach (var item in Cache)                Remove(item.Key);        }    }}

2. Expansion

Using System; namespace using iseframe. core. caching {/// <summary> /// Extensions // </summary> public static class CacheExtensions {/// <summary> /// obtain the cache, if the acquire result does not exist, it is saved to the cache, the default time is 60 Minutes. // </summary> // <typeparam name = "T"> Type </typeparam> // <param name = "cacheManager"> Cache manager </param> /// <param name = "key"> Cache key </param> /// <param name = "acquire"> Function to load item if it's not in the cache yet </param> // <returns> Cached item </returns> public static T Get <T> (this ICacheManager cacheManager, string key, Func <T> acquire) {return Get (cacheManager, key, 60, acquire) ;}/// <summary> /// obtain the cache. If no cache exists, load and cache the execution result. /// </summary> /// <typeparam name = "T"> cache type </typeparam> /// <param name =" cacheManager "> Cache manager </param> /// <param name =" key "> Cache key </param> /// <param name =" cacheTime "> the Cache time is 0 minutes-do not cache </param> /// <param name = "acquire"> if no cache exists, execute this expression to set the cache </param> /// <returns> Cached item </returns> public static T Get <T> (this ICacheManager cacheManager, string key, int cacheTime, Func <T> acquire) {if (cacheManager. isSet (key) {return cacheManager. get <T> (key) ;}else {var result = acquire (); if (cacheTime> 0) cacheManager. set (key, result, cacheTime); return result ;}}}}

 

3. MemoryCacheManager implementation

using System;using System.Collections.Generic;using System.Runtime.Caching;using System.Text.RegularExpressions;namespace EnterpriseFrame.Core.Caching{    /// <summary>    /// Represents a manager for caching between HTTP requests (long term caching)    /// </summary>    public partial class MemoryCacheManager : ICacheManager    {        protected ObjectCache Cache        {            get            {                return MemoryCache.Default;            }        }                /// <summary>        /// Gets or sets the value associated with the specified key.        /// </summary>        /// <typeparam name="T">Type</typeparam>        /// <param name="key">The key of the value to get.</param>        /// <returns>The value associated with the specified key.</returns>        public virtual T Get<T>(string key)        {            return (T)Cache[key];        }        /// <summary>        /// Adds the specified key and object to the cache.        /// </summary>        /// <param name="key">key</param>        /// <param name="data">Data</param>        /// <param name="cacheTime">Cache time</param>        public virtual void Set(string key, object data, int cacheTime)        {            if (data == null)                return;            var policy = new CacheItemPolicy();            policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);            Cache.Add(new CacheItem(key, data), policy);        }        /// <summary>        /// Gets a value indicating whether the value associated with the specified key is cached        /// </summary>        /// <param name="key">key</param>        /// <returns>Result</returns>        public virtual bool IsSet(string key)        {            return (Cache.Contains(key));        }        /// <summary>        /// Removes the value with the specified key from the cache        /// </summary>        /// <param name="key">/key</param>        public virtual void Remove(string key)        {            Cache.Remove(key);        }        /// <summary>        /// Removes items by pattern        /// </summary>        /// <param name="pattern">pattern</param>        public virtual void RemoveByPattern(string pattern)        {            var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);            var keysToRemove = new List<String>();            foreach (var item in Cache)                if (regex.IsMatch(item.Key))                    keysToRemove.Add(item.Key);            foreach (string key in keysToRemove)            {                Remove(key);            }        }        /// <summary>        /// Clear all cache data        /// </summary>        public virtual void Clear()        {            foreach (var item in Cache)                Remove(item.Key);        }    }}

From: NopCommerce framework (http://nopcommerce.codeplex.com /)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.