[转]C#操作Memcached帮助类
在VS中安装Memcached,直接在NuGet下搜索Memcached,选择第一个进行安装:
服务端资源下载地址:https://pan.baidu.com/s/1gf3tupl
接下来开始写程序,老规矩,直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
public class Memcached : ICache { private static readonly MemcachedClient CacheClient = new MemcachedClient(); /// <summary> /// 获取缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cacheKey"></param> /// <returns></returns> public T GetCache<T>( string cacheKey) where T : class { try { return (T)CacheClient.Get(cacheKey); } catch { return default (T); } } /// <summary> /// 写入缓存,过期时间默认10分钟 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <param name="cacheKey"></param> public void WriteCache<T>(T value, string cacheKey) where T : class { //CacheClient.Store(StoreMode.Set, cacheKey, value); CacheClient.Store(Exists(cacheKey) ? StoreMode.Set : StoreMode.Replace, cacheKey, value, DateTimeHelper.Now.AddMinutes(10)); } /// <summary> /// 写入缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <param name="cacheKey"></param> /// <param name="expireTime"></param> public void WriteCache<T>(T value, string cacheKey, DateTime expireTime) where T : class { //CacheClient.Store(StoreMode.Set, cacheKey, value, expireTime); CacheClient.Store(Exists(cacheKey) ? StoreMode.Set : StoreMode.Replace, cacheKey, value, expireTime); } /// <summary> /// 移除缓存 /// </summary> /// <param name="cacheKey"></param> public void RemoveCache( string cacheKey) { CacheClient.Remove(cacheKey); } /// <summary> /// 移除所有缓存 /// </summary> public void RemoveCache() { CacheClient.FlushAll(); } /// <summary> /// 是否存在 /// </summary> /// <param name="key"></param> /// <returns></returns> private static bool Exists( string key) { return CacheClient.Get(key) != null ; } } |
接口类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
public interface ICache { /// <summary> /// 读取缓存 /// </summary> /// <param name="cacheKey">键</param> /// <returns></returns> T GetCache<T>( string cacheKey) where T : class ; /// <summary> /// 写入缓存 /// </summary> /// <param name="value">对象数据</param> /// <param name="cacheKey">键</param> void WriteCache<T>(T value, string cacheKey) where T : class ; /// <summary> /// 写入缓存 /// </summary> /// <param name="value">对象数据</param> /// <param name="cacheKey">键</param> /// <param name="expireTime">到期时间</param> void WriteCache<T>(T value, string cacheKey, DateTime expireTime) where T : class ; /// <summary> /// 移除指定数据缓存 /// </summary> /// <param name="cacheKey">键</param> void RemoveCache( string cacheKey); /// <summary> /// 移除全部缓存 /// </summary> void RemoveCache(); } |
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<configSections> <section name= "unity" type= "Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" /> <section name= "entityFramework" type= "System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission= "false" /> <sectionGroup name= "enyim.com" > <section name= "memcached" type= "Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /> </sectionGroup> </configSections> <enyim.com> <memcached> <servers> <add address= "192.168.1.12" port= "11211" /> </servers> <socketPool minPoolSize= "10" maxPoolSize= "1000" connectionTimeout= "00:00:10" deadTimeout= "00:02:00" /> </memcached> </enyim.com> |
---------------------
作者:大师兄丶
来源:CNBLOGS
原文:https://www.cnblogs.com/zhao-yi/p/7826672.html
版权声明:本文为作者原创文章,转载请附上博文链接!