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;
            }
        }
    }

  • 相关阅读:
    php and web service with wsdl
    QT creator出现 no executable specified时解决办法
    ECShop 2.x 3.0代码执行漏洞分析
    关于QtCharts中的映射器与模型的使用
    QT 异步函数转为同步函数的方法
    Win7+Qt5.6.0(64位)+msvc2015编译器 环境配置
    SMTP用户枚举原理简介及相关工具
    Android系统广播机制存在漏洞,恶意软件可绕过安全机制跟踪用户
    Qt 维护工具MaintenanceTool.exe 使用
    漏洞预警 | ECShop全系列版本远程代码执行高危漏洞
  • 原文地址:https://www.cnblogs.com/movemoon/p/4755227.html
Copyright © 2011-2022 走看看