使用C#AppFabric缓存实例 [英] AppFabric caching examples using c#

查看:428
本文介绍了使用C#AppFabric缓存实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究AppFabirc缓存融入我的.NET C#应用程序,并寻找这样的一些code的例子。是否有任何开源或code可提供样品关AppFabric缓存可用,我可以看看?

I am currently researching the integration of AppFabirc caching into my .net c# application and looking for some code examples of such. Are there any open source or code samples available off AppFabric caching available that I can look at?

推荐答案

缓存操作结果
第一个对象创建时处理AppFabric缓存是一个 DataCacheFactory 。这可以用,告诉工厂如何与缓存服务器,或者没有配置在这种情况下,从你的web.config / app.config文件读取配置硬件codeD的配置数据来创建。我的建议是,你保持你的配置信息,在你的config文件,否则当你想改变的东西在缓存中的设置,你需要重新编译和重新分发应用程序。要记住的DataCacheFactory重要的是它的昂贵的创建 - 你绝对不希望创建其中之一为每个高速缓存操作。考虑使用Singleton模式 - 请参阅<一个href=\"http://stackoverflow.com/questions/4498675/where-to-store-appfabric-datacachefactory-in-asp-net\">this问题了解更多详情。

Cache Operations
The first object to create when dealing with AppFabric caching is a DataCacheFactory. This can be created either with hard-coded configuration data that tells the factory how to contact the cache server, or with no configuration in which case it reads the configuration from your web.config/app.config file. My recommendation is that you keep your config information in your .config file, otherwise when you want to change something in your cache setup, you'll need to re-compile and re-distribute your application. The important thing to remember about the DataCacheFactory is it is expensive to create - you definitely don't want to create one of these for every cache operation. Consider using the Singleton pattern - see this question for more details.

// Create a factory reading the config info from the .config file
DataCacheFactory factory = new DataCacheFactory();

主界面缓存是通过缓存对象。从DataCacheFactory的 GetCache 方法获得的缓存,通过缓存中的名称:

The main interface to a cache is through the Cache object. A Cache is obtained from the DataCacheFactory's GetCache method, passing in the name of the cache:

DataCache myCache = factory.GetCache("myCache");

添加项目到Cache结果
在一个高速缓存中的每个项目都有一个键,这是一个字符串。密钥必须是唯一的高速缓存 - 如果你在​​一个已经存在,你会得到一个例外,一键通。要缓存的项目必须是serialisable所以AppFabric的内部可以通过它在高速缓存中的服务器周围。在最基本的层面上,项目采用添加到缓存中的添加方法。

object myCachedItem = new Object();
string myCachedItemKey = "MyCacheKey";
myCache.Add(myCachedItemKey, myCachedItem);

从缓存中删除项目

Removing an Item from the Cache

myCache.Remove(myCachedItemKey);

就这么简单。

获得一个项目从缓存结果
当从缓存获取的项目,我们使用缓存预留模式。这意味着我们期待在高速缓存,以查看是否需要的项目是在那里(用键)。如果该项目是在缓存中,我们采取的缓存项(它有可能转换为不同的类型);否则我们会采取措施来获得从头例如项目从数据库中读取,然后缓存它,所以它是给我们下一次。

Getting an Item From the Cache
When getting an item from the cache, we use the cache-aside pattern. This means we look in the cache to see if the desired item is there (using the key). If the item is in the cache, we take the cached item (potentially casting it to a different type); otherwise we take steps to get the item from scratch e.g. reading from a database, and then cache it so it is there for us next time.

object cachedObject;
string myImportantDataKey = "MyImportantDataTable";
DataTable myImportantData;

// This is an object because everything is returned from the cache as an object
cachedObject = myCache.Get(myImportantDataKey);
// Check to see if we got something from the cache
if (cachedObject == null)
{
    // The requested item wasn't in the cache - a cache miss
    // Go get the data from the db
    myImportantData = getMyImportantDataFromMyDatabase();

    // Add the item to the cache so we've got it for next time
    myCache.Add(myImportantDataKey, myImportantData);
}
else
{
    // We have the cached object so cast it to a DataTable
    myImportantData = (DataTable)cachedObject;
}

在缓存更新的项目

// Put can be used to update an existing item in the cache
// If the item does not exist then it functions like Add
myCache.Put(myImportantDataKey, myUpdatedImportantData);

这是基本的CRUD操作,但有很多更多,你可以进入处理并发!

That's the basic CRUD operations, but there's plenty more that you can get into for dealing with concurrency!

将Windows Server AppFabric的培训工具包可以下载<一个href=\"http://www.microsoft.com/downloads/en/details.aspx?FamilyID=7290f7ed-e86b-4114-a452-4f07fa32403d\">here,它有一个部分
缓存。这里保留继AppFabric的标记作为我敢肯定,随着时间的推移就会有解决问题的人有更多的code样品。

The Windows Server AppFabric Training Kit can be downloaded here, it has a section on caching. Keep following the appfabric tag here as I'm sure over time there'll be many more code samples solving problems for people.

这篇关于使用C#AppFabric缓存实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆