asp.net mvc json数据缓存
一些虚拟主机资源给的少, 如果直接用框架缓存, 估计内存就爆了吧, 如果不用缓存, 虚拟主机自带的数据库也是限制资源的, 访问多了就直接给timeout了,
用json文件形式缓存查询出来的数据, 虽然占用一些空间, 使用一些IO, 也是比查询数据库快的, 至少不会timeout了, 不过会占用一些空间, 可是现在虚拟主机空间都不小了, 不差这一点了哈哈,
key直接作为文件名了,扩展名乱写的(*.cache)
超时时间比较用的是DateTime.Ticks, 单位是微妙, 看下面的换算吧, 在AppSetting中增加一个cache_time节点, 单位是秒
<appSettings>
<!-- 缓存时间, 单位(秒) -->
<add key="cache_time" value="120" />
</appSettings>
public class JCache
{
public static string DirPath { get; set; }
private static string SplitChar = "!/~/#!";
/// <summary>
/// 缓存时间(秒)
/// </summary>
private static long CacheTime
{
get
{
return Convert.ToInt64(ConfigurationManager.AppSettings["cache_time"]) * ;
}
}
public static T Get<T>(string key)
where T : class, new()
{
var filename = Path.Combine(DirPath, key + ".cache");
if(!Directory.Exists(DirPath))
{
Directory.CreateDirectory(DirPath);
}
if (!File.Exists(filename))
return null;
var fileContent = string.Empty;// File.ReadAllText(filename, System.Text.Encoding.UTF8);
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
fileContent = new StreamReader(fs, System.Text.Encoding.UTF8).ReadToEnd();
}
if (string.IsNullOrEmpty(fileContent))
return null;
if (!fileContent.Contains(SplitChar))
return null;
var arr = fileContent.Split(new string[] { SplitChar }, StringSplitOptions.RemoveEmptyEntries);
try
{
var d = Convert.ToInt64(arr[]);
if (DateTime.Now.Ticks - d > CacheTime)
return null; var res = JsonConvert.DeserializeObject<T>(arr[]);
return res;
}
catch (Exception ex)
{
return null;
}
} public static void Set(string key, object obj)
{
if (!Directory.Exists(DirPath))
{
Directory.CreateDirectory(DirPath);
}
var filename = Path.Combine(DirPath, key + ".cache");
var d = DateTime.Now.Ticks.ToString();
if (obj == null)
return;
var json = JsonConvert.SerializeObject(obj);
var fileContent = string.Concat(d, SplitChar, json);
try
{
using (var fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
new StreamWriter(fs, System.Text.Encoding.UTF8).Write(fileContent);
}
//File.WriteAllText(filename, fileContent);
}
catch (Exception ex)
{
var xx = ;
}
}
}
使用方法
var cahceKey = "xxxkey";
var cache = JCache.Get<T>(cacheKey); var model = new T();
if(cache == null){
JCache.Set<T>(cacheKey, 查询出来的数据);
}else{
model = cache;
}
asp.net mvc json数据缓存的更多相关文章
- ASP.NET MVC 数据库依赖缓存
ASP.NET MVC 数据库依赖缓存 问题背景 最近做一个非常简单的功能,就是使用ajax请求的方式从服务端请求一段下拉表的数据. 以前也有做过这个功能,只不过这次做这个功能的时候冒出了一个想法 ...
- Asp.net MVC 传递数据 从前台到后台,包括单个对象,多个对象,集合
今天为大家分享下 Asp.net MVC 将数据从前台传递到后台的几种方式. 环境:VS2013,MVC5.0框架 1.基本数据类型 我们常见有传递 int, string, bool, double ...
- ASP.NET MVC中将数据从Controller传递到视图
ASP.NET MVC中将数据从Controller传递到视图方法 1.ViewData ViewData的类型是字典数据,key-value 如:ViewData["Data"] ...
- 解决ASP.NET MVC(post数据)Json请求太大,无法反序列化(The JSON request was too large to be deserialized)
这个问题出现的场景并不是很多,当你向服务端异步(ajax)post数据非常大的情况下(比如做权限管理的时候给某个角色分配权限那么就可能会出现,我所遇到的就是该角色大概200个模块每个模块平均2个功能- ...
- ASP.NET MVC Json() 处理大数据异常解决方法 json MaxJsonLength
网上很多解决方案,在webconfig中添加,但是实践证明无效 <system.web.extensions> <scripting> <webServices> ...
- ASP.NET WEBAPI 简单CURD综合测试(asp.net MVC,json.net,sql基础存储过程和视图,sqlhelper,json解析)
草图 真正的后端是不管前端是什么平台,用什么语言的,JSON格式的数据应该可以应对.用ASP.NET WEBAPI尝试做一个后端,实现最基本的CURD,业务逻辑和数据库操作都放在后端,前端只需要正 ...
- 初遇 Asp.net MVC 数据库依赖缓存那些事儿
问题背景: 最近做一个非常简单的功能,就是使用ajax请求的方式从服务端请求一段下拉表的数据. 以前也有做过这个功能,只不过这次做这个功能的时候冒出了一个想法: 我请求的这段数据它是一段相对比较固定的 ...
- ASP.NET MVC 数据库依赖缓存的实现
当数据库中的信息发生变化的时候,应用程序能够获取变化的通知是缓存依赖得以实现的基础.应用程序可以通过轮询获取数据变化的信息,使用轮询的话也不可能重新查一次后再和以前的数据做比较,如果这样的话如果我一个 ...
- Asp.NET MVC JSON序列化问题
最近在做项目的时候遇到一个JSON序列化问题. 环境:ASP.NET MVC 4.0 数据库:SQL 2008 在将获取的数据从后台以json的形式传给前台的easyui treegrid绑定的时候通 ...
随机推荐
- python 普通文件读写
with open('ttt.txt', 'w') as f: f.write('456.098909,9.090988,7.878765') with open('ttt.txt', 'r') as ...
- 查看 rospkg 变量
echo $ROS_PACKAGE_PATH
- 快速幂模n运算
模运算里的求幂运算,比如 5^596 mod 1234, 当然,直接使用暴力循环也未尝不可,在书上看到一个快速模幂算法 大概思路是,a^b mod n ,先将b转换成二进制,然后从最高位开始(最高位一 ...
- 创意时钟 人形时钟 可惜不是 https
; (function () { $('#header').css({ 'position':'relative' }).prepend('<div id="clockWrap&quo ...
- Linux访问windows共享(samba/smbclient/smbfs/cifs)
samba是一个实现不同操作系统之间文件共享和打印机共享的一种SMB协议的免费软件.●安装samba,samba-client和cifs-utils.x86_64此步将自动安装好相关依赖包:samba ...
- js事件轮询机制
console.log(1) setTimeout(function(){ console.log(2) },0); console.log(3) 毫无疑问:运行结果是1 3 2 也就是说:setTi ...
- telnet 命令使用方法详解,telnet命令怎么用?
什么是Telnet? 对于Telnet的认识,不同的人持有不同的观点,可以把Telnet当成一种通信协议,但是对于入侵者而言,Telnet只是一种远程登录的工具.一旦入侵者与远程主机建立了Telnet ...
- 如何将新项目添加到github仓库中?只需简单几步~即可实现
问题描述:新建了一个项目,如何将其设置为git项目?如何关联到github上的仓库? 只需简单几步,但前提是需要已经安装好了git,并且有github账户 本文使用IntelliJ IDEA 其他编辑 ...
- 记录一个错误,在bundle install时候出现 shoulda-mathcers bundle install fails with git error
复制粘体错误到google.找到解决方案: https://github.com/thoughtbot/shoulda-matchers/issues/1057 GIT remote: https:/ ...
- Matlab scatter 如何显示不同颜色点状
有时候需要在matlab scatter绘图中显示不同颜色区分,如下图是人体血压高压.低压与年龄关系的散点图. 红色点表示高压 绿色点表示低压 用 matlab 如何实现呢? 1.创建一维矩阵x,y1 ...