Memcached Tip 1:使用Memcached Providers
Memcached的Windows版本在这里下载http://code.google.com/p/memcached/wiki/PlatformWindows(或http://memcachedproviders.codeplex.com/),最高的版本是1.4.4,但是已经很难在网上下载到。不过http://www.couchbase.com/对Memcached进行了很好的维护,但是这个团队把Memcached封装进了自己的产品中(1.4.4也是由这个团队开发的,但是它们已经移除了1.4.4的Memcached的独立分支)。我没有试过couchbase的产品,故本例针对1.2.6(你也可以从本站下载memcached.rar,反正只有几十K)。
1:将Memcached注册进Windows服务
memcached -d install
2:启动Memcached服务
memcached -d start
3:停止和移除Memcached服务
memcached -d stop
memcached -d uninstall
4:查看Memcached状态
使用
telnet localhost 11211
然后,输入stats,如果有如下结果就表示安装成功了:
5:Memcached客户端
Memcached的.NET客户端,比较著名的就是http://memcachedproviders.codeplex.com/,它其实封装的是enyim.com Memcached Client。选取Memcached Providers 1.2 .NET 3.5。
6:一个简单的示例
创建一个Winform,引用客户端包中的3个DLL,为其设定App.config,如下:
<?xml version="1.0"?> <configuration> <configSections> <section name="cacheProvider" type="MemcachedProviders.Cache.CacheProviderSection, MemcachedProviders" allowDefinition="MachineToApplication" restartOnExternalChanges="true"/> <sectionGroup name="enyim.com"> <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching"/> </sectionGroup> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <log4net> <!-- Define some output appenders --> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/> </layout> </appender> <!--<threshold value="OFF" />--> <!-- Setup the root category, add the appenders and set the default priority --> <root> <priority value="WARN"/> <appender-ref ref="ConsoleAppender"> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="WARN"/> <levelMax value="FATAL"/> </filter> </appender-ref> </root> </log4net> <enyim.com> <memcached> <servers> <!-- put your own server(s) here--> <add address="127.0.0.1" port="11211"/> </servers> <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00"/> </memcached> </enyim.com> <cacheProvider defaultProvider="MemcachedCacheProvider"> <providers> <add name="MemcachedCacheProvider" type="MemcachedProviders.Cache.MemcachedCacheProvider, MemcachedProviders" keySuffix="_MySuffix_" defaultExpireTime="2000"/> </providers> </cacheProvider> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
同时,我们的主程序如下:
private void buttonSet_Click(object sender, EventArgs e) { DistCache.Add(this.textBoxKey.Text, this.textBoxValue.Text); this.textBoxKey.BackColor = Color.White; this.textBoxValue.BackColor = Color.White; MessageBox.Show("设置成功"); } private void buttonGet_Click(object sender, EventArgs e) { object obj = DistCache.Get(this.textBoxKey.Text); if (obj != null) { this.textBoxValue.Text = obj.ToString(); this.textBoxKey.BackColor = Color.Gray; this.textBoxValue.BackColor = Color.Gray; } else { this.textBoxValue.Text = "没有这个Key值"; this.textBoxKey.BackColor = Color.Gray; this.textBoxValue.BackColor = Color.Gray; } }
注意using MemcachedProviders.Cache;
我们也可以很容易将此程序应用到Asp.net中。