一些虚拟主机资源给的少, 如果直接用框架缓存, 估计内存就爆了吧, 如果不用缓存, 虚拟主机自带的数据库也是限制资源的, 访问多了就直接给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数据缓存的更多相关文章

  1. ASP.NET MVC 数据库依赖缓存

    ASP.NET MVC 数据库依赖缓存   问题背景 最近做一个非常简单的功能,就是使用ajax请求的方式从服务端请求一段下拉表的数据. 以前也有做过这个功能,只不过这次做这个功能的时候冒出了一个想法 ...

  2. Asp.net MVC 传递数据 从前台到后台,包括单个对象,多个对象,集合

    今天为大家分享下 Asp.net MVC 将数据从前台传递到后台的几种方式. 环境:VS2013,MVC5.0框架 1.基本数据类型 我们常见有传递 int, string, bool, double ...

  3. ASP.NET MVC中将数据从Controller传递到视图

    ASP.NET MVC中将数据从Controller传递到视图方法 1.ViewData ViewData的类型是字典数据,key-value 如:ViewData["Data"] ...

  4. 解决ASP.NET MVC(post数据)Json请求太大,无法反序列化(The JSON request was too large to be deserialized)

    这个问题出现的场景并不是很多,当你向服务端异步(ajax)post数据非常大的情况下(比如做权限管理的时候给某个角色分配权限那么就可能会出现,我所遇到的就是该角色大概200个模块每个模块平均2个功能- ...

  5. ASP.NET MVC Json() 处理大数据异常解决方法 json MaxJsonLength

    网上很多解决方案,在webconfig中添加,但是实践证明无效 <system.web.extensions> <scripting> <webServices> ...

  6. ASP.NET WEBAPI 简单CURD综合测试(asp.net MVC,json.net,sql基础存储过程和视图,sqlhelper,json解析)

    草图   真正的后端是不管前端是什么平台,用什么语言的,JSON格式的数据应该可以应对.用ASP.NET WEBAPI尝试做一个后端,实现最基本的CURD,业务逻辑和数据库操作都放在后端,前端只需要正 ...

  7. 初遇 Asp.net MVC 数据库依赖缓存那些事儿

    问题背景: 最近做一个非常简单的功能,就是使用ajax请求的方式从服务端请求一段下拉表的数据. 以前也有做过这个功能,只不过这次做这个功能的时候冒出了一个想法: 我请求的这段数据它是一段相对比较固定的 ...

  8. ASP.NET MVC 数据库依赖缓存的实现

    当数据库中的信息发生变化的时候,应用程序能够获取变化的通知是缓存依赖得以实现的基础.应用程序可以通过轮询获取数据变化的信息,使用轮询的话也不可能重新查一次后再和以前的数据做比较,如果这样的话如果我一个 ...

  9. Asp.NET MVC JSON序列化问题

    最近在做项目的时候遇到一个JSON序列化问题. 环境:ASP.NET MVC 4.0 数据库:SQL 2008 在将获取的数据从后台以json的形式传给前台的easyui treegrid绑定的时候通 ...

随机推荐

  1. dos与unix系统的格式转化

    unix 只用\n作为行结束符,而在 dos中是以\r和\n作为行结束符, 如果一个文件是在unix系统下创建,然后想在dos下使用,就要用unix2dos,如 unix2dos file 如果一个文 ...

  2. XML_CPP_资料_libXml2_01_Code

    ZC: 这里的代码,就是 http://www.cnblogs.com/cppskill/p/6207609.html(我的文章"XML_CPP_资料_libXml2_01 - CppSki ...

  3. node 工程化 web项目

    1.结构 node_modules   ( ... ) routers     ( main.js  ) views    ( index.html   about.HTML  404.html ) ...

  4. robot 批处理文件

    robot自带的ride工具不好用,就像填表格似的写脚本,太拘束.所以一直在用sublime text写robot脚本,但是也有问题:用sublime text写的脚本,只能运行一个文件的case,并 ...

  5. 雷林鹏分享:Ruby 注释

    Ruby 注释 注释是在运行时会被忽略的 Ruby 代码内的注释行.单行注释以 # 字符开始,直到该行结束,如下所示: #!/usr/bin/ruby -w # 这是一个单行注释. puts &quo ...

  6. 20170609批量生成WORD合同

    Sub NextSeven_CodeFrame() Application.ScreenUpdating = False Application.DisplayAlerts = False Appli ...

  7. 1月4日编程基础hash

    早上git加星了webapp的教程 > h = Hash.new('Go Fishing')       => {}  // 创建一个空hash,设定"Go Fishing&qu ...

  8. CentOS 7 install Nginx

    1. rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.r ...

  9. python-day27--hashlib模块-摘要算法

    1.用途: # 文件校验 # 文件是否被改变# 登录密码 #不能解密,但可以“撞库” #加盐 hashlib.md5('nezha'.encode('utf-8')) 2. import hashli ...

  10. zzuli303(奇葩26进制转换)

    序号互换 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐标快速计算出来.单元格的行坐标是由数字 ...