application与cache

每个项目都有一些全局,常用的信息,而这些信息如果在每次使用时都载入,那必将耗费很大的资源,特别是对访问压力大的系统。因此,这个情况中,把这些全局信息放到缓存中是很必要的,放在缓存中可以使得数据能够很快的被读取,节省了很多宝贵的CPU和IO。
项目中通常是用application 和cache来实现缓存的功能。他们的用法分别为:

1)application:
application["test"] = "this is a application message!";

2)cache

Cache.Add("Key1", "Value");

两种用法都很相似,都是采用名/值对的方式来保存数据,而在读取数据时也只要用 键 就可以获取缓存的值。

而2种相比,到底哪种更有优势呢? 答案是CACHE在使用上更具有灵活性。特点如下:

1。自有的按时更新缓存的机制

有的项目需要定时获取最新数据的需求,如天气预报,可能间隔10分钟 就要读取一次需求,那这可以利用CACHE本身的方法来实现。

//监视某个时间
public void CreateDependency(Object sender, EventArgs e) {
    // Create a DateTime object.
    DateTime dt = DateTime.Now.AddSeconds(10);

// Create a cache entry.
    Cache["key1"] = "Value 1";
    CacheDependency dependency = new CacheDependency(null,  dt);

Cache.Insert("key2", "Value 2", dependency);

DisplayValues();
}

2.当缓存的源修改时,可以重新更新缓存。这个缓存源可以是变量,也可以是文件,或者目录。

//监视某个变量
public void CreateDependency(Object sender, EventArgs e) {
    // Create a DateTime object.
    //DateTime dt = DateTime.Now.AddSeconds(10);

// Create a cache entry.
    Cache["key1"] = "Value 1";

// Make key2 dependent on key1.
    String[] dependencyKey = new String;
    dependencyKey[0] = "key1";
    CacheDependency dependency = new CacheDependency(null, dependencyKey, null);

Cache.Insert("key2", "Value 2", dependency);

DisplayValues();
}

3.同时以多种搭配来自动更新缓存,如同时监视某个文件,并且在指定间隔的时间内自动更新。

//监视某个时间和变量
public void CreateDependency(Object sender, EventArgs e) {
    // Create a DateTime object.
    DateTime dt = DateTime.Now.AddSeconds(10);

// Create a cache entry.
    Cache["key1"] = "Value 1";

// Make key2 dependent on key1.
    String[] dependencyKey = new String;
    dependencyKey[0] = "key1";
    CacheDependency dependency = new CacheDependency(null, dependencyKey, dt);

Cache.Insert("key2", "Value 2", dependency);

DisplayValues();
}

比起APPLICATION来,CACHE更显得灵活。

时间: 2024-08-26 03:33:08

application与cache的相关文章

[Asp.Net]状态管理(Session、Application、Cache、Cookie 、Viewstate、隐藏域 、查询字符串)

Session:  1. 客户在服务器上第一次打开Asp.Net页面时,会话就开始了.当客户在20分钟之内没有访问服务器,会话结束,销毁session.(当然也可以在Web.config中设置缓存时间)可以在Global.aspx的Session_Start()事件处理程序中,可以初始化会话变量.在下面的实例中,名为mydata的会话状态被初始化为0: 运行结果: 2.Session是保存在服务器端的用户变量.我可以在一个页面中对Session进行值,然后在另一个页面里访问它. Session的

[.net 面向对象程序设计进阶] (14) 缓存(Cache) (一) 认识缓存技术

[.net 面向对象程序设计进阶] (14) 缓存(Cache)(一) 认识缓存技术 本节导读: 缓存(Cache)是一种用空间换时间的技术,在.NET程序设计中合理利用,可以极大的提高程序的运行效率. 本节将介绍如何利用缓存写出高效率的代码. 1. 什么是缓存(Cache)? 缓存(Cache)是一种用空间换取时间的技术 存在于计算机中很多地方,用来将一些慢速设备中的常用数据保存在快速设备中,取数据的时候直接从快速设备中取. 比如CPU二级缓存.内存.windows文件读取缓存. 2. .NE

[中英对照]Why Redis beats Memcached for caching | 在cache化方面,为何Redis胜过Memcached?

对Memcached和Redis有兴趣的同学不妨花几分钟读一读本文,否则请飘过. Why Redis beats Memcached for caching | 在cache化方面,为何Redis胜过Memcached? Memcached is sometimes more efficient, but Redis is almost always the better choice. XXX Memcached or Redis? It's a question that nearly al

内容和图片在从网络上获取到之后都会存入到本地缓存中

内容和图片在从网络上获取到之后都会存入到本地缓存中,因此即使手机在没有网络的情况下依然能够加载出以前浏览过的新闻.而使用的缓存技术不用多说,自然是DiskLruCache了,那么首先第一个问题,这些数据都被缓存在了手机的什么位置呢? 其实DiskLruCache并没有限制数据的缓存位置,可以自由地进行设定,但是通常情况下多数应用程序都会将缓存的位置选择为 /sdcard/Android/data/<application package>/cache 这个路径.选择在这个位置有两点好处:第一,

Bootstrap Table Fixed Columns

最近在使用BootStrap 做项目前端,自然也用到了  Bootstrap Table. 推荐大家多去这个http://bootstrap-table.wenzhixin.net.cn/zh-cn/  网站看看,上面有很详细的介绍以及其他扩展功能 下面写下 Fixed Columns(固定列)的使用方法.附件下载地址:http://pan.baidu.com/s/1kUEQTPt 1.引用css文件,js文件(注意引用顺序) <link rel="stylesheet" hre

android API文档查询---context、toast、SharedPreferences

/*查阅api ---context1.abstract AssetManager     getAssets() Returns an AssetManager instance for the application's package. 得到assets目录下的资源 2.abstract File     getCacheDir() Returns the absolute path to the application specific cache directory on the fi

硬盘缓存技术DiskLruCache技术&lt;笔记&gt;

防止多图OOM的核心解决思路就是使用LruCache技术,但LruCache只是管理了内存中图片的存储与释放,如果图片从内存中被移除的话,那么又需要从网络上重新加载一次,这显然非常耗时.因此Google又提供了一套硬盘缓存的解决方案:DiskLruCache(非Google官方编写,但获得官方认证).一般来说新闻类App从网络获取到数据后都会存入到本地缓存中,因此即使手机在没有网络的情况下依然能够加载出以前浏览过的新闻.使用的缓存技术自然是DiskLruCache.以网易新闻为例,它的Andro

Environment中有大量访问目录的函数

public class Environment { /** * Return root of the "system" partition holding the core Android OS. * Always present and mounted read-only. */ public static File getRootDirectory() /** * Return root directory of the "oem" partition hol

图片处理工具类

1 package com.example.util; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapF