分类目录

链接

2017年 1月
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
最新asp.net/c#使用Memcached完整示例及注意事项
.NET 暂无评论 阅读(1,099)

目录

 

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.写了一个帮助类

 

  1. /// <summary>
  2.     /// CacheHelper
  3.     /// </summary>
  4.     public class CacheHelper
  5.     {
  6.         private static readonly MemcachedClient CacheClient = new MemcachedClient();
  7.  
  8.         /// <summary>
  9.         /// Retrieve cached item
  10.         /// </summary>
  11.         /// <typeparam name="T">Type of cached item</typeparam>
  12.         /// <param name="key">Name of cached item</param>
  13.         /// <returns>Cached item as type</returns>
  14.         public static T Set<T>(string key, T objectToCache) where T : class
  15.         {
  16.             try
  17.             {
  18.                 bool status = false;
  19.                 if (Get<T>(key) == null)
  20.                 {
  21.                     status = CacheClient.Store(StoreMode.Set, key, objectToCache);
  22.                 }
  23.                 else
  24.                 {
  25.                     status = CacheClient.Store(StoreMode.Replace, key, objectToCache);
  26.                 }
  27.                 if (status == false)
  28.                 {
  29.                     return null;
  30.                 }
  31.                 return objectToCache;
  32.             }
  33.             catch
  34.             {
  35.                 return null;
  36.             }
  37.         }
  38.         public static object Set(string key, object objectToCache)
  39.         {
  40.             try
  41.             {
  42.                 bool status = false;
  43.                 if (Get(key) == null)
  44.                 {
  45.                     status = CacheClient.Store(StoreMode.Set, key, objectToCache);
  46.                 }
  47.                 else
  48.                 {
  49.                     status = CacheClient.Store(StoreMode.Replace, key, objectToCache);
  50.                 }
  51.                 if (status == false)
  52.                 {
  53.                     return null;
  54.                 }
  55.                 return objectToCache;
  56.             }
  57.             catch
  58.             {
  59.                 return null;
  60.             }
  61.         }
  62.  
  63.  
  64.         /// <summary>
  65.         /// Retrieve cached item
  66.         /// </summary>
  67.         /// <typeparam name="T">Type of cached item</typeparam>
  68.         /// <param name="key">Name of cached item</param>
  69.         /// <returns>Cached item as type</returns>
  70.         public static T Get<T>(string key) where T : class
  71.         {
  72.             try
  73.             {
  74.                 return (T)CacheClient.Get(key);
  75.             }
  76.             catch
  77.             {
  78.                 return null;
  79.             }
  80.         }
  81.         public static object Get(string key)
  82.         {
  83.             try
  84.             {
  85.                 return CacheClient.Get(key);
  86.             }
  87.             catch
  88.             {
  89.                 return null;
  90.             }
  91.         }
  92.         /// <summary>
  93.         /// Insert value into the cache using
  94.         /// appropriate name/value pairs
  95.         /// </summary>
  96.         /// <typeparam name="T">Type of cached item</typeparam>
  97.         /// <param name="objectToCache">Item to be cached</param>
  98.         /// <param name="key">Name of item</param>
  99.         /// <param name="cacheDuration">Duration of the cache.</param>
  100.         public static void Add<T>(T objectToCache, string key, int cacheDuration) where T : class
  101.         {
  102.             CacheClient.Store(StoreMode.Set, key, objectToCache, DateTime.Now.AddMinutes(cacheDuration));
  103.         }
  104.       ...
  105.      //未完,详情见 下载

4.最关键的一步是 配置文件了

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3.   <configSections>
  4.     <section name="FineUI" type="FineUI.ConfigSection, FineUI" requirePermission="false" />
  5.     <section name="linq2db" type="LinqToDB.Configuration.LinqToDBSection, linq2db" requirePermission="false" />
  6.     <sectionGroup name="enyim.com">
  7.       <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
  8.     </sectionGroup>
  9.   </configSections>
  10.   <enyim.com>
  11.     <memcached>
  12.       <servers>
  13.         <add address="127.0.0.1" port="11211" />
  14.       </servers>
  15.       <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" />
  16.     </memcached>
  17.   </enyim.com>
  18. ...其它你自己的配置未写完

 

5.使用示例

  1. //login success
  2.  CacheHelper.Set("user", user);
  3.  var userCache = CacheHelper.Get("user");

注意:存储对象时,对像必须加上 [Serializable] 可序列化。

差不多就到这里了,自己写来备第一次使用同学参考。有问题留言,有时间一定回答。

 

6.DEMO下载

哦, 忘记了,最重要的: Memcached-DEMO

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:最新asp.net/c#使用Memcached完整示例及注意事项 | Bruce's Blog

发表评论

留言无头像?