使用 c# 的 AppFabric 缓存示例 [英] AppFabric caching examples using c#

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

问题描述

我目前正在研究将 AppFabirc 缓存集成到我的 .net c# 应用程序中,并寻找一些此类代码示例.是否有可用的 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 文件中读取配置.我的建议是将配置信息保存在 .config 文件中,否则当您想更改缓存设置中的某些内容时,您需要重新编译并重新分发您的应用程序.关于 DataCacheFactory 需要记住的重要一点是创建它昂贵 - 您绝对不想为每个缓存操作创建其中一个.考虑使用单例模式 - 请参阅这个问题了解更多详情.

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();

缓存的主要接口是通过 Cache 对象.从 DataCacheFactory 的 GetCache 方法中获取一个 Cache,传入缓存的名称:

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");

向缓存中添加项目
缓存中的每个项目都有一个键,它是一个字符串.键对于缓存必须是唯一的——如果你传入一个已经存在的键,你会得到一个异常.要缓存的项目必须是可序列化的,以便 AppFabric 可以在缓存中的服务器内部传递它.在最基本的层面上,使用 Add 方法将项目添加到缓存中.

Adding An Item to the Cache
Each item in a cache has a key, which is a string. The key must be unique to the cache - if you pass in a key that already exists you'll get an exception. The item to be cached must be serialisable so AppFabric can internally pass it around the servers in the cache. At the most basic level, items are added to the cache using the Add method.

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

从缓存中删除一个项目

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 培训工具包 这里,它有一个关于缓存.继续关注这里的 appfabric 标签,因为我相信随着时间的推移,会有更多代码示例为人们解决问题.

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天全站免登陆