zoukankan      html  css  js  c++  java
  • 缓存


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Threading;
    using System.Web.Caching;

    namespace WebApiDemo.Common
    {
        public class CacheHelper
        {
            public static object Get(string cacheKey)
            {
                return HttpRuntime.Cache[cacheKey];
            }
            public static void Add(string cacheKey, object obj, int cacheMinute)
            {
                HttpRuntime.Cache.Insert(cacheKey, obj, null, DateTime.Now.AddMinutes(cacheMinute),
                    Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            }
        }

        public class MyCache
        {
            public object GetMemberSigninDays5()
            {
                const int cacheTime = 5;
                const string cacheKey = "mushroomsir";

                //缓存标记。
                const string cacheSign = cacheKey + "_Sign";
                var sign = CacheHelper.Get(cacheSign);

                //获取缓存值
                var cacheValue = CacheHelper.Get(cacheKey);
                if (sign != null)
                    return cacheValue; //未过期,直接返回。

                lock (cacheSign)
                {
                    sign = CacheHelper.Get(cacheSign);
                    if (sign != null)
                        return cacheValue;

                    CacheHelper.Add(cacheSign, "1", cacheTime);
                    ThreadPool.QueueUserWorkItem((arg) =>
                    {
                        cacheValue = "395"; //这里一般是 sql查询数据。 例:395 签到天数
                        CacheHelper.Add(cacheKey, cacheValue, cacheTime * 2); //日期设缓存时间的2倍,用于脏读。
                    });
                }
                return cacheValue;
            }
        }
    }

  • 相关阅读:
    汇编学习笔记(3)[bx]和loop
    C++面试题-概念篇(一)
    命名空间的冷思考
    背包以及装备模块封装的思考
    虚函数,纯虚函数以及虚继承
    组件化开发在游戏开发当中的思考和汇总
    Netty和MINA之间的比较思考
    学习C++与Java之间的区别
    C++服务器年前总结
    C++Builder如何将当前时间与字符串相互转换
  • 原文地址:https://www.cnblogs.com/movemoon/p/4755227.html
Copyright © 2011-2022 走看看