缓存
2015-08-24 16:50
281 查看
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;
}
}
}
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;
}
}
}
相关文章推荐
- LAMP搭建
- npm(node)的安装步骤
- JAVA 得到两日期相差几个月
- 关于"引用"的几点说明
- GDI双缓冲绘图
- PL/SQL Developer调试存储过程,函数
- Spring JdbcTemplate的batchUpdate中,没有看到conn.setAutoCommit(false)的操作
- Http Header之User-Agent
- 黑马程序员_面向对象之封装
- jboss CLI 命令行接口学习(适用JBOSS EAP 6.2+)
- 企业模式之Unit Of Work模式
- 关于C++中的友元函数的总结
- c++运算符重载
- Hadoop学习之Ubuntu12.04 Hadoop 环境搭建笔记
- webService的基础知识一
- C++ Primer 5e chapter 15.3
- C++中endl
- ftp缓存信息
- 什么是JavaScript闭包终极全解之一——基础概念
- c++运算符重载