在10分钟内安装,配置和使用Windows Server AppFabric和“ Velocity”内存缓存

A few weeks back I blogged about the Windows Server AppFabric launch (AppFabric is Microsoft's "Application Server") and a number of folks had questions about how to install and configure the "Velocity" memory cache. It used to be kind of confusing during the betas but it's really easy now that it's released.

几周前,我在博客上发表了有关Windows Server AppFabric启动的信息(AppFabric是Microsoft的“应用程序服务器”),许多人都对如何安装和配置“ Velocity”内存缓存存有疑问。 在测试版中它曾经使人感到困惑,但是现在发布它确实很容易。

Here's the comment:

这是评论:

Have you tried to setup a appfabric (velocity) instance ? I suggest you try & even do a blog post, maybe under the scenario of using it like a memcache for dasblog. I would love to know how to setup it up, it's crazy hard for what it is.

您是否尝试设置一个appfabric(速度)实例? 我建议您尝试甚至撰写博客文章,也许是在像dasblog的内存缓存一样使用它的情况下。 我很想知道如何设置它,对于它来说这太疯狂了。

No problem, happy to help. I won't do it for dasblog, but I'll give you easy examples that'll take about 10 minutes.

没问题,乐于帮助。 我不会在dasblog上这样做,但是我将给您提供一些简单的示例,这些示例大约需要10分钟。

获取并安装AppFabric (Get and Install AppFabric)

You can go to http://msdn.com/appfabric and download it directly or just do it with the Web Platform Installer.

您可以转到http://msdn.com/appfabric 直接下载它,仅使用Web Platform Installer进行下载

Run the installer and select AppFabric Cache. If you're on Windows 7, you'll want to install the IIS 7 Manager for Remote Administration which is a little plugin that lets you manage remote IIS servers from your Windows 7 machine.

运行安装程序,然后选择AppFabric缓存。 如果您使用的是Windows 7,则需要安装用于远程管理IIS 7管理器,它是一个小插件,可让您从Windows 7计算机上管理远程IIS服务器。

NOTE: You can also an automated/unattended installation as well via SETUP /i CACHINGSERVICE to just get caching.

注意:您也可以通过SETUP / i CACHINGSERVICE自动/无人值守安装来进行缓存。

The configuration tool will pop up, and walk you through a small wizard. You can setup AppFabric Hosting Services for Monitoring and Workflow Persistence, but since I'm just doing Caching, I'll skip it.

将会弹出配置工具,并引导您完成一个小向导。 您可以设置AppFabric Hosting Services来进行监视和工作流持久性,但是由于我只是在进行缓存,因此我将跳过它。

The Velocity Caching Service needs to know where to get its configuration and it can get it from one of two places - either a database or an XML file on a share. If you use the XML file on a share, you'll need to make sure the service account has access to the share, etc. I'll use a database. The config wizard can make it for you as well. Click Next then Finish up the configuration.

Velocity Caching Service需要知道从何处获取其配置,并且可以从以下两个位置之一获取它:数据库或共享中的XML文件。 如果您在共享上使用XML文件,则需要确保服务帐户有权访问共享等。我将使用数据库。 配置向导也可以帮您实现。 单击下一步,然后完成配置。

Configuring the Configuration Database...

配置配置数据库...

Ok, let's start it up and poke around.

好吧,让我们启动它并四处看看。

从PowerShell启动和管理内存群集 (Start and Administer your Memory Cluster from PowerShell)

Now what? Go to the Start Menu and type in Caching. You'll have an item called "Caching Administration Windows PowerShell." This is where you can connect to the cache, check out what's going on, make new caches, etc. Run it as Administrator.

怎么办? 转到“开始”菜单,然后键入“缓存”。 您将拥有一个名为“缓存管理Windows PowerShell”的项目。 在这里,您可以连接到缓存,查看发生了什么,建立新的缓存,等等。以管理员身份运行。

If you type "get-command *cache*" you'll see all the different commands available for cache management. I typed start-cachecluster.

如果键入“ get-command * cache *”,您将看到所有可用于缓存管理的不同命令。 我输入了start-cachecluster。

C:\> Start-CacheCluster

C:\> Start-CacheCluster

HostName : CachePort      Service Name            Service Status Version Info
--------------------      ------------            -------------- ------------
HANSELMAN-W500:22233      AppFabricCachingService UP             1 [1,1][1,1]

HostName:CachePort服务名称服务状态版本信息-------------------- ------------ -------------- ---- -------- HANSELMAN-W500:22233 AppFabricCachingService UP 1 [1,1] [1,1]

Cool, it's up and running. If you look in the config database (or the XML file if you chose that) you'll see that I have one machine in my memory cluster. I could have lots and lots, and if I had Windows Server Enterprise I would also have high-availability if one of the nodes went down.

很酷,它已经启动并正在运行。 如果查看配置数据库(如果选择的话,则查看XML文件),您会看到我的内存集群中有一台计算机。 我可以有很多很多,如果我有Windows Server Enterprise,并且其中一个节点出现故障,我也将具有高可用性。

I download the AppFabric Caching Samples and opened the CacheSampleWebApp in Visual Studio. Immediately we notice the two new references we don't usually see in a web application, Microsoft.ApplicationServer.Caching.Core and .Client.

我下载了AppFabric缓存示例,并在Visual Studio中打开了CacheSampleWebApp。 立即我们注意到在Web应用程序中通常不会看到的两个新引用Microsoft.ApplicationServer.Caching.Core和.Client。

image

Remember that for security everything is locked down by default, so you'll need to grant access to the cache for whatever user you'll be using to access it. I'm running as "ScottHa" so I'll run

请记住,出于安全性考虑,默认情况下所有内容都处于锁定状态,因此您需要为要使用的任何用户授予对缓存的访问权限。 我以“ ScottHa”身份运行,所以我将运行

Grant-CacheAllowedClientAccount scottha

Grant-CacheAllowedClientAccount scottha

...and you should do the same for whatever account your IIS is running as.

...并且您应该对运行IIS的任何帐户执行相同的操作。

从ASP.NET使用您的内存缓存 (Use Your Memory Cache from ASP.NET)

Remember that you can chop up your memory caches into logical buckets (partitions) and a memory cluster can serve more than one application, if you wanted.

请记住,您可以将内存缓存划分为逻辑存储区(分区),并且如果需要,一个内存集群可以为多个应用程序提供服务。

Your cache can be hooked up in the web.config or from code (however you like). Here's a code example helper method where the sample does this manually. This data could come from wherever you like, you just need to tell it a machine to talk to and the portnumber. It'll automatically connect to the

您的缓存可以连接到web.config或代码中(无论如何)。 这是一个代码示例帮助程序方法,其中示例手动执行此操作。 这些数据可能来自您喜欢的任何地方,您只需要告诉它要与之对话的机器和端口号即可。 它将自动连接到

Caches can also be partitioned. For example, I'm using a named cache called "default" but I could have multiple logically segmented areas like "shoppingcart" and "productcatalog" if I wanted.

缓存也可以分区。 例如,我使用的是一个名为“默认”的命名缓存,但是如果需要的话,我可以有多个逻辑上分段的区域,例如“ shoppingcart”和“ productcatalog”。

using Microsoft.ApplicationServer.Caching;
using System.Collections.Generic;

public class CacheUtil
{
private static DataCacheFactory _factory = null;
private static DataCache _cache = null;

public static DataCache GetCache()
{
if (_cache != null)
return _cache;

//Define Array for 1 Cache Host
List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>(1);

//Specify Cache Host Details
// Parameter 1 = host name
// Parameter 2 = cache port number
servers.Add(new DataCacheServerEndpoint("mymachine", 22233));

//Create cache configuration
DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();

//Set the cache host(s)
configuration.Servers = servers;

//Set default properties for local cache (local cache disabled)
configuration.LocalCacheProperties = new DataCacheLocalCacheProperties();

//Disable tracing to avoid informational/verbose messages on the web page
DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);

//Pass configuration settings to cacheFactory constructor
_factory = new DataCacheFactory(configuration);

//Get reference to named cache called "default"
_cache = _factory.GetCache("default");

return _cache;
}
}

Once your cache is setup, it's trivial to use.

设置好缓存后,就可以轻松使用它了。

m_cache.Add(orderid, order);

and

Order order = (Order)m_cache.Get(orderid);

or updating an existing object:

或更新现有对象:

m_cache.Put(orderid, order);

检查您的缓存统计信息 (Check your Caching Statistics)

So after adding a bunch of items to the cache, then requesting a bunch back I can go into PowerShell and see what's going on:

因此,在将一堆项目添加到缓存后,再请求一堆,我可以进入PowerShell,看看发生了什么:

C:\> get-cache

C:\>获取缓存

CacheName            [Host]
                     Regions
---------            ---------------
default              [HANSELMAN-W500:22233]
                     Default_Region_0103(Primary)

CacheName [主机] 地区--------- --------------- 默认[HANSELMAN-W500:22233] Default_Region_0103(主要)

C:\> Get-CacheStatistics default

C:\> Get-CacheStatistics默认

Size         : 2493
ItemCount    : 5
RegionCount  : 5
RequestCount : 17
MissCount    : 3

大小:2493 物品数:5 RegionCount:5 RequestCount:17 小姐数:3

You can use Performance Monitor as there is an imperial buttload of different Performance Counters Available. As I mentioned, you can make different partitions, like "default" or "poopypants" and check the stats on each of those separate, or the cache as a whole:

您可以使用性能监视器,因为有大量不同的性能计数器供您选择。 如前所述,您可以创建不同的分区,例如“默认”或“ poopypants”,并检查每个单独分区或整个缓存上的统计信息:

AppFabric Velocity Caching in PerfMon

And of course, I can recycle my webserver, start it up again and fetch an order and it's still there. You've effectively got a big, partitionable distributed (and optionally highly available) hashtable across multiple machines.

当然,我可以回收我的Web服务器,重新启动它并获取订单,它仍然存在。 您实际上已经在多台计算机上获得了一个大型的,可分区的分布式(并且可选地高度可用)哈希表。

Diagram Explaining what AppFabric looks like as an architecture

用AppFabric缓存替换ASP.NET会话状态 (Replacing ASP.NET Session State with AppFabric Caching)

If you want, in ASP.NET 4 you can also swap out the default in-memory Session State Provider for AppFabric via your web.config. Here's an example web.config.

如果需要,还可以ASP.NET 4中通过web.config换出AppFabric的默认内存中会话状态提供程序。 这是一个示例web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<!--configSections must be the FIRST element -->
<configSections>
<!-- required to read the <dataCacheClient> element -->
<section name="dataCacheClient"
type="Microsoft.ApplicationServer.Caching.DataCacheClientSection,
Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
allowLocation="true"
allowDefinition="Everywhere"/>
</configSections>

<!-- cache client -->
<dataCacheClient>
<!-- cache host(s) -->
<hosts>
<host
name="CacheServer1"
cachePort="22233"/>
</hosts>
</dataCacheClient>

<system.web>
<sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
<providers>
<!-- specify the named cache for session data -->
<add
name="AppFabricCacheSessionStoreProvider"
type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider"
cacheName="poopylands"
sharedId="MySharedApp"/>
</providers>
</sessionState>
</system.web>
</configuration>

资源和链接 (Resources and Links)

Here's a recent AppFabric caching slidedeck from Ron Jacobs I found useful. More links below. Microsoft Windows Server AppFabric Slides at SlideShare.

这是我发现有用的Ron Jacobs最近发布的AppFabric缓存幻灯片。 下面的更多链接。 SlideShare上的Microsoft Windows Server AppFabric幻灯片

As with all things, a little abstraction goes a long way. If you have an existing caching strategy (via EntLib, or whatever) you can almost certainly swap out your internal storage for AppFabric Caching.

与所有事物一样,稍微抽象也会很有效。 如果您有现有的缓存策略(通过EntLib或其他方法),则几乎可以肯定地将内部存储换成AppFabric缓存。

Related Links

相关链接

翻译自: https://www.hanselman.com/blog/installing-configuring-and-using-windows-server-appfabric-and-the-velocity-memory-cache-in-10-minutes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值