惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
N
News | PayPal Newsroom
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
S
Secure Thoughts
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 【当耐特】
博客园 - 聂微东
S
Securelist
宝玉的分享
宝玉的分享
爱范儿
爱范儿
IT之家
IT之家
T
The Exploit Database - CXSecurity.com
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
AI
AI
Security Latest
Security Latest
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Last Watchdog
The Last Watchdog
月光博客
月光博客
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
人人都是产品经理
人人都是产品经理
Webroot Blog
Webroot Blog
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
N
News and Events Feed by Topic
Recent Commits to openclaw:main
Recent Commits to openclaw:main
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
S
Security Affairs
美团技术团队
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
罗磊的独立博客
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
T
Tailwind CSS Blog
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - HelloSnoopy

写了个用一个文件定义样式的DataGrid 一个最基本的WebService+Flash调用的实例 [转载]如何奔向程序员打工的出头之日? 一定要讲给孩子们的20个小故事 人脉对职业生涯最重要 (转) 爱情的10种样子,送给懂爱的朋友们 装了Enterprise Library.NET 200501release C#判断是否为数字的最好速度! 开源的 BugTracker.net 错误跟踪管理系统 Enjoy new year of 2005! Flash AS2 用EventDispatcher广播事件 设计模式随笔-让众口不再难调 CodeSmith使用心得 [专题]MVC构架模式 用一条SQL完成数据表的行统计 如何使IFrame的长宽与内容自动适应大小 为Project提供一个统一风格的DataGrid 用于读取树形任一节点下所有级别子节点的SqlServer UDF asp.net forums中定时器的应用
使用Microsoft Application Block之Cache Application
HelloSnoopy · 2004-12-26 · via 博客园 - HelloSnoopy

关于caching application block可以从http://www.microsoft.com/resources/practices/default.mspx下载

这里我们介绍一下简单的使用

1.建立一个windows form的测试程序

textbox用来显示cache的内容,button用来获取cache项.程序不能在简单了哦.

2.添加Cache的引用(在block的安装目录,直接copy过来):

Microsoft.ApplicationBlocks.Cache.dll
Microsoft.ApplicationBlocks.Data.dll
Microsoft.ApplicationBlocks.ExceptionManagement.dll
Microsoft.ApplicationBlocks.ExceptionManagement.Interfaces.dll
MemoryMappedFileStream.dll

3.配置config文件

因为是win form,所以添加一个app.config,如果是web引用,则对应web.config文件.

默认的文件示例:C:\Program Files\Microsoft Application Blocks for .NET\Caching Framework\Code\CS\Solution Items

这里我们直接copy app.config的内容吧.

好了,所有部署就完成了,下面来看看code

using System.Data;
using Microsoft.ApplicationBlocks.Cache;

namespace WinApp_Block_Cache
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.TextBox textBox1;

        
private void button1_Click(object sender, System.EventArgs e)
        
{
            
this.textBox1.Text = (string)GetCachedItem("hello");    
        }

        
private object GetCachedItem(string itemName)
        
{
            
//获取Cache管理的singlton
            CacheManager cacheMgr = CacheManager.GetCacheManager();
            
object obj = cacheMgr.GetData(itemName);
            
if(obj == null)
            
{
                
//下面是ExtendedFormatTime设置释义
                /// <remarks>
                
/// Extended format sintax : <br/><br/>
                
/// 
                
/// Minute       - 0-59 <br/>
                
/// Hour         - 0-23 <br/>
                
/// Day of month - 1-31 <br/>
                
/// Month        - 1-12 <br/>
                
/// Day of week  - 0-7 (Sunday is 0 or 7) <br/>
                
/// Wildcards    - * means run every <br/>
                
/// Examples: <br/>
                
/// * * * * *    - expire every minute of every day <br/>
                
/// 5 * * * *    - expire 5th minute of every hour or every minute
                
///                   after 1 hour from the last used time, whichever
                
///                   is earlier <br/>
                
/// * 21 * * *   - expire every minute of the 21st hour of every day 
                
///                   or one hour after every day<br/>
                
/// 31 15 * * *  - expire 3:31 PM every day <br/>
                
/// 7 4 * * 6    - expire Saturday 4:07 AM <br/>
                
/// 15 21 4 7 *  - expire 9:15 PM on 4 July <br/>
                
/// </remarks>

                Microsoft.ApplicationBlocks.Cache.ICacheItemExpiration[] exps = 
                    
{
                        
//设置cache过期依赖,这里设置1分钟后过期.
                        
//你可以设置多个过期依赖项,该block提供如下四种过期依赖:
                        
//AbsoluteTime
                        
//ExtendedFormatTime
                        
//FileDependency
                        
//SlidingTime

                        
new Microsoft.ApplicationBlocks.Cache.Expirations.ExtendedFormatTime("* * * * *")
                    }
;
                obj 
= "hello world!!" + DateTime.Now.Millisecond.ToString();

                
//如果不想对cache过期事件处理,可把callback设为null
                
//这里我们用CacheExpiredProcess来处理cache的过期
                cacheMgr.Add(itemName,
                    obj,
                    exps,
                    CacheItemPriority.High,
                    
new CacheItemRemovedCallback(CacheExpiredProcess));
            }

            
return obj;
        }

        
//设置了一个处理过期事件函数
        private void CacheExpiredProcess(string cacheKey,CacheItemRemoveCause cause)
        
{
            MessageBox.Show(cacheKey 
+ " is expired!");
        }


    }

}