最新asp.net/c#使用Memcached完整示例及注意事项
目录
1.下载及安装Memcached
(本文使用的是windows x64版本,linux同样,后期结出asp.net+Memcached在LINUX下的使用教程)
Memcached的下载安装及使用我就不多说了,网上很多。
服务的启动:
(1) 将memcached-1.2.1-win32.zip解决到指定的地方,如c:\memcached
(2) 命令行输入 d:\memcached\memcached.exe -d install
(3) 命令行输入 d:\memcached\memcached.exe -d start ,该命令启动 Memcached,默认监听端口为 11211
可以通过 memcached.exe -h 可以查看其帮助
2.准备.NET要用的DLL
下载这两个DLL,版本要一致,如图(示例代码及DLL在文章结尾下载):
3.写了一个帮助类
- /// <summary>
- /// CacheHelper
- /// </summary>
- public class CacheHelper
- {
- private static readonly MemcachedClient CacheClient = new MemcachedClient();
- /// <summary>
- /// Retrieve cached item
- /// </summary>
- /// <typeparam name="T">Type of cached item</typeparam>
- /// <param name="key">Name of cached item</param>
- /// <returns>Cached item as type</returns>
- public static T Set<T>(string key, T objectToCache) where T : class
- {
- try
- {
- bool status = false;
- if (Get<T>(key) == null)
- {
- status = CacheClient.Store(StoreMode.Set, key, objectToCache);
- }
- else
- {
- status = CacheClient.Store(StoreMode.Replace, key, objectToCache);
- }
- if (status == false)
- {
- return null;
- }
- return objectToCache;
- }
- catch
- {
- return null;
- }
- }
- public static object Set(string key, object objectToCache)
- {
- try
- {
- bool status = false;
- if (Get(key) == null)
- {
- status = CacheClient.Store(StoreMode.Set, key, objectToCache);
- }
- else
- {
- status = CacheClient.Store(StoreMode.Replace, key, objectToCache);
- }
- if (status == false)
- {
- return null;
- }
- return objectToCache;
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// Retrieve cached item
- /// </summary>
- /// <typeparam name="T">Type of cached item</typeparam>
- /// <param name="key">Name of cached item</param>
- /// <returns>Cached item as type</returns>
- public static T Get<T>(string key) where T : class
- {
- try
- {
- return (T)CacheClient.Get(key);
- }
- catch
- {
- return null;
- }
- }
- public static object Get(string key)
- {
- try
- {
- return CacheClient.Get(key);
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// Insert value into the cache using
- /// appropriate name/value pairs
- /// </summary>
- /// <typeparam name="T">Type of cached item</typeparam>
- /// <param name="objectToCache">Item to be cached</param>
- /// <param name="key">Name of item</param>
- /// <param name="cacheDuration">Duration of the cache.</param>
- public static void Add<T>(T objectToCache, string key, int cacheDuration) where T : class
- {
- CacheClient.Store(StoreMode.Set, key, objectToCache, DateTime.Now.AddMinutes(cacheDuration));
- }
- ...
- //未完,详情见 下载
4.最关键的一步是 配置文件了
- <?xml version="1.0" encoding="utf-8"?>
- <configuration>
- <configSections>
- <section name="FineUI" type="FineUI.ConfigSection, FineUI" requirePermission="false" />
- <section name="linq2db" type="LinqToDB.Configuration.LinqToDBSection, linq2db" requirePermission="false" />
- <sectionGroup name="enyim.com">
- <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
- </sectionGroup>
- </configSections>
- <enyim.com>
- <memcached>
- <servers>
- <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>
- ...其它你自己的配置未写完
5.使用示例
- //login success
- CacheHelper.Set("user", user);
- var userCache = CacheHelper.Get("user");
注意:存储对象时,对像必须加上 [Serializable] 可序列化。
差不多就到这里了,自己写来备第一次使用同学参考。有问题留言,有时间一定回答。
6.DEMO下载
哦, 忘记了,最重要的: Memcached-DEMO
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · 使用jenkins远程布署.net项目
- · ASP.NET MVC分页 demo
- · c#/ASP.NET向百度站长平台主动推送URL
- · ASP.NET分页组装HTML代码
- · linux asp.net用httpWebRequest或WebClient访问https站点
- · 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求
- · 支持高并发的IIS Web服务器常用设置
- · LINUX下部署ASP.NET:无需安装Mono的Jexus“独立版”
- · windows下memcached cpu 100%可能的原因
- · 讨论:关于ASP.NET用Timer定时执行问题
- · ASP.NET获取工程根目录
- · Asp.Net里的Cookies用法