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 matplot画散列图
同matlab一样,matplot也可画散列图scatter. import numpy as np import matplotlib.pyplot as plt #fig = plt.figure ...
- c++ 指定长度容器元素的拷贝(copy_n)
#include <iostream> // cout #include <algorithm> // copy #include <vector> ...
- c++ 满足条件拷贝,容器扩容(copy_if)
#include <iostream> // cout #include <algorithm> // copy_if, distance #include <vecto ...
- Android JNI学习(二)——实战JNI之“hello world”
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
- 【Golang 接口自动化07】struct转map的三种方式
背景 我们在前面介绍过怎么使用net/http发送json或者map数据,那么它能不能直接发送结构体数据呢?我们今天一起来学习结构体struct转map的三种方法,为后续做铺垫. struct转map ...
- ln软连接
ln软连接 ln -s 源目录/文件 目标目录/文件 例如,有个应用 /var/www/html/webapp,下面有个logs日志文件夹,想吧 webapp/logs日志文件夹链到/home ...
- android之视频播放系统VideoView和自定义VideoView控件的应用
Android播放视频,包含系统自带VideoView控件,和自定义VideoView控件,可全屏播放,案例包含了本地视频和网络视频. 1:自定义VideoView控件 2:布局代码 3:Activi ...
- Tomcat报错Exception: java.lang.OutOfMemoryError
进入Tomcat中的/bin/catalina.sh 在catalina.sh中echo"Using CATALINA_BASE"之前的一行添加如下代码: JAVA_OPTS=&q ...
- Charles安装破解、抓取https请求及常见问题解决方法
现在基本大部分网站都使用了https,所以要想抓到https的请求,首要任务是先有工具:charles.fiddler,先介绍下charles针对https请求的抓取方法,此方法兼容windows和m ...
- hdu多校2C
题意:找多条路径覆盖所有的边,求最小路径数,要求输出路径 题解:新建一个点n+1,所有奇点向它连边,然后跑欧拉回路,最后把新加的边删去,一段连续的边就是一条路径 = =但是由于太久没写欧拉回路以及之前 ...