Cashe的使用

1.CacheHelper
public class CacheHelper
    {
        public static ObjectCache Cache
        {
            get
            {
                return MemoryCache.Default;
            }
        }


        public static bool TryGetCache<T>(string key, ref T value)
        {
            object v = null;
            //Type t = typeof(T);
            bool hit;
            hit = TryGetCacheObject(key, ref v);
            if (hit)
                value = (T)v;
            return hit;
        }

        //public static bool TryGetCache(string key, ref bool value)
        //{
        //    return TryGetCacheStruct(key, ref value);
        //}

        //public static bool TryGetCache(string key, ref int value)
        //{
        //    return TryGetCacheStruct(key, ref value);
        //}

        public static bool TryGetCacheStruct<T>(string key, ref T value) where T : struct
        {
            object v = null;
            bool hit = TryGetCacheObject(key, ref v);
            if (hit)
                value = (T)v;
            return hit;
        }

        public static bool TryGetCacheObject(string key, ref object value)
        {
            object v = Cache.Get(key);
            bool hit = false;
            if (v == null)
                hit = false;
            else if (v == DBNull.Value)
            {
                hit = true;
                value = null;
            }
            else
            {
                hit = true;
                value = v;
            }
            //TraceHelper.Trace("Cache", string.Format("TryGetCache({0}) = {1}", key, hit));
            return hit;
        }


        public static bool ContainsCache(string key)
        {
            bool hit = Cache.Contains(key);
            //TraceHelper.Trace("Cache", string.Format("ContainsCache({0}) = {1}", key, hit));
            return hit;
        }

        public static object GetCache(string key)
        {
            object v = Cache.Get(key);
            if (v == DBNull.Value)
            {
                return null;
            }
            //TraceHelper.Trace("Cache", string.Format("GetCache({0}) = {1}", key, v == null ? "null" : v.ToString()));
            return v;
        }

        public static void SetCache(string key, object value)
        {
            SetCache(key, value, CacheItemPolicy);
        }

        public static void SetCache(string key, object value, CacheItemPolicy policy)
        {
            object v = value;
            if (value == null)
                v = DBNull.Value;
            Cache.Set(key, v, policy);
            //TraceHelper.Trace("Cache", string.Format("SetCache({0}) = {1}", key, value == null ? "null" : value.ToString()));
        }

        public static CacheItemPolicy CacheItemPolicy
        {
            get
            {
                
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.SlidingExpiration = new TimeSpan(0, AppConfiguration.CacheSlidingExpirationInMins, 0);
                return policy;
            }
        }

        public static CacheItemPolicy AbsoluteCacheItemPolicy
        {
            get
            {
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(AppConfiguration.CacheAbsoluteExpirationInMins);
                return policy;
            }
        }

        public static void ClearCacheByPrefix(string prefix)
        {
            List<string> keys = new List<string>();
            foreach (var c in Cache)
            {
                if (c.Key.StartsWith(prefix))
                {
                    keys.Add(c.Key);
                }
            }
            int count = keys.Count;
            foreach (var key in keys)
            {
                Cache.Remove(key);
            }
            //TraceHelper.Trace("Cache", string.Format("ClearCacheByPrefix({0}) = {1}", prefix, count));
        }
    }


 

    public class TestKey
    {
        public string Code { get; set; }
        public decimal CodeNo { get; set; }

        public override bool Equals(object obj)
        {
            TestKey v = obj as TestKey;
            if (v == null) return false;

            return v.Code== this.Code && v.CodeNo== this.CodeNo;
        }

        public override int GetHashCode()
        {
            int primeNo = 31;
            return (this.Code.GetHashCode() * primeNo + this.CodeNo.GetHashCode();
        }
    }


 

        public static HashSet<StopPaymentKey> GetAllFromCache()
        {
            string cachekey = "AllTestKeys";
            HashSet<TestKey> set = null;
            if (!CacheHelper.TryGetCache(cachekey, ref set))
            {
                set= GetAllTestKeys();
                CacheItemPolicy policy = CacheHelper.AbsoluteCacheItemPolicy;
                //policy.RemovedCallback = (arg) =>
                //{
                //    var newSet = GetAllTestCodes();
                //    CacheHelper.SetCache(cachekey, newSet, policy);
                //};
                CacheHelper.SetCache(cachekey, stoppaymentSet, policy);
            }
            return set;
        }


 

 

转载于:https://www.cnblogs.com/sui84/p/6777035.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JetCache 是一个基于 Java 的开源缓存库,它提供了对方法级缓存的支持。使用 JetCache 可以方便地将方法的结果缓存起来,提高应用程序的性能和响应速度。 下面是使用 JetCache 的基本步骤: 1. 添加 JetCache 依赖:将 JetCache 的依赖项添加到项目的构建文件中(如 Maven 或 Gradle)。 2. 配置缓存:在应用程序的配置文件中,配置需要使用的缓存类型和相关属性。例如,可以配置内存缓存、Redis 缓存等。 3. 注解方法:在需要进行缓存的方法上添加 JetCache 的注解,如 `@Cached`、`@CacheRemove` 等。这些注解可以指定缓存的 key、过期时间、条件等。 4. 使用缓存:在调用被注解的方法时,JetCache 会根据注解的配置自动处理缓存。如果缓存中存在所需数据,则直接返回缓存数据;否则,执行方法并将结果放入缓存。 下面是一个简单的示例: ```java import io.github.jiashunx.cache.Cache; import io.github.jiashunx.cache.annotation.Cached; public class MyService { private Cache<String, String> cache; // 构造函数或依赖注入注入 Cache 实例 @Cached(name = "myCache", key = "#param", expire = 600) public String getData(String param) { // 从数据库或其他数据源中获取数据 // ... return data; } } ``` 在上述示例中,`MyService` 使用了 JetCache 的 `@Cached` 注解对 `getData` 方法进行了缓存配置。缓存的名称为 "myCache",缓存的 key 使用方法参数 `param`,缓存的过期时间为 600 秒。当调用 `getData` 方法时,JetCache 会自动处理缓存逻辑,如果缓存中存在对应的数据,则直接返回缓存数据;否则,执行方法并将结果放入缓存。 这只是 JetCache 的基本用法,JetCache 还提供了其他更复杂的缓存策略和配置选项,可以根据具体需求进行配置和使用。 希望这个回答对您有帮助!如有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值