1、创建个人MemcachedHelper类

  

  1. /// <summary>
  2. /// 页 面 名:缓存管理类<br/>
  3. /// 说 明:设置、获取、移除Cache<br/>
  4. /// 作 者:xxx<br/>
  5. /// 时 间:2012-12-12(神奇的数字,传说中的12K)<br/>
  6. /// 修 改 者:<br/>
  7. /// 修改时间:<br/>
  8. ///</summary>
  9. public class MemcachedHelper
  10. {
  11.  
  12. #region 变量和构造函数
  13. //缓存服务器地址和端口,这样就实现了分布式缓存。服务器可以多个,多个时使用逗号(,)分开,用Socket读写数据"127.0.0.1:12345,127.0.0.2:12345",
  14. private static Dictionary<string, string> Servers = new Dictionary<string, string>();
  15. static MemcachedClient mc = null;
  16. //服务器缓存
  17. static Dictionary<string, SockIOPool> Servers1 = new Dictionary<string, SockIOPool>();
  18. static object LOCK_OBJECT = new object();//安全锁定
  19. /// <summary>
  20. /// 静态构造函数
  21. /// </summary>
  22. static MemcachedHelper()
  23. {
  24. //初始化服务器列表
  25. InitServer();
  26. List<string> keys = Servers.Keys.ToList<string>();
  27. foreach (var k in Servers.Keys)
  28. {
  29. SockIOPool pool = SockIOPool.GetInstance(k);
  30. string[] s = new string[] { Servers[k] };
  31. pool.SetServers(s);//设置服务器
  32. pool.MaxConnections = ; //最大连接数
  33. pool.MinConnections = ; //最小连接数
  34. pool.SocketConnectTimeout = ; //Socket连接超时设置
  35. pool.SocketTimeout = ; //
  36. pool.HashingAlgorithm = HashingAlgorithm.NewCompatibleHash; //传输算法
  37. pool.Initialize();//初始化缓存线程池
  38. }
  39. //默认池
  40. List<string> defaultServerList = new List<string>();
  41. foreach (var k in Servers.Keys)
  42. {
  43. defaultServerList.Add(Servers[k]);
  44. }
  45. SockIOPool defaultPool = SockIOPool.GetInstance("DefaultPool");
  46. defaultPool.SetServers(defaultServerList.ToArray());//设置服务器
  47. defaultPool.MaxConnections = ;
  48. defaultPool.MinConnections = ;
  49. defaultPool.SocketConnectTimeout = ;
  50. defaultPool.SocketTimeout = ;
  51. defaultPool.Initialize(); //初始化默认线程池
  52. mc = new MemcachedClient();
  53. mc.PoolName = "DefaultPool";
  54. }
  55. /// <summary>
  56. /// 初始化服务器列表,这里默认两台服务器.
  57. /// </summary>
  58. static void InitServer()
  59. {
  60. //这里可以写复杂灵活点,动态从配置文件获取,我这里固定了两个
  61. Servers.Add("memservers", ConfigurationManager.AppSettings["memservers"]);
  62. // Servers.Add("Svr2", ConfigurationManager.AppSettings["Svr2"]);
  63. }
  64. private MemcachedHelper() { }
  65. #endregion
  66.  
  67. #region 获取客户端,客户端使用单例模式生成
  68. public static MemcachedClient GetClient(string server)
  69. {
  70. //MemcachedClient current = Singleton<MemcachedClient>.Instance;
  71. MemcachedClient current = Singleton<MemcachedClient>.GetInstance();
  72. current.PoolName = server;
  73. return current;
  74. }
  75. #endregion
  76.  
  77. #region 默认
  78.  
  79. #region 写(Set)
  80. /// <summary>
  81. /// 设置数据缓存
  82. /// </summary>
  83. /// <param name="key">键</param>
  84. /// <param name="value">值</param>
  85. public static void Set(string key, object value)
  86. {
  87. mc.Set(key, value);
  88. }
  89. /// <summary>
  90. /// 设置数据缓存
  91. /// </summary>
  92. /// <param name="key">键</param>
  93. /// <param name="value">值</param>
  94. /// <param name="hashCode">哈希码</param>
  95. public static void Set(string key, object value, int hashCode)
  96. {
  97. mc.Set(key, value, hashCode);
  98. }
  99. /// <summary>
  100. /// 设置数据缓存
  101. /// </summary>
  102. /// <param name="key">键</param>
  103. /// <param name="value">值</param>
  104. /// <param name="expiry">过期时间</param>
  105. public static void Set(string key, object value, DateTime expiry)
  106. {
  107. mc.Set(key, value, expiry);
  108. }
  109. /// <summary>
  110. /// 设置数据缓存
  111. /// </summary>
  112. /// <param name="key">键</param>
  113. /// <param name="value">值</param>
  114. /// <param name="expiry">过期时间</param>
  115. public static void Set(string key, object value, DateTime expiry, int hashCode)
  116. {
  117. mc.Set(key, value, expiry, hashCode);
  118. }
  119. #endregion
  120.  
  121. #region 读(Get)
  122.  
  123. #region 返回泛型
  124. /// <summary>
  125. /// 读取数据缓存
  126. /// </summary>
  127. /// <param name="key">键</param>
  128. public static T Get<T>(string key)
  129. {
  130. return (T)mc.Get(key);
  131. }
  132. /// <summary>
  133. /// 读取数据缓存
  134. /// </summary>
  135. /// <param name="key">键</param>
  136. /// <param name="hashCode">哈希码</param>
  137. public static T Get<T>(string key, int hashCode)
  138. {
  139. return (T)mc.Get(key, hashCode);
  140. }
  141. /// <summary>
  142. /// 读取数据缓存
  143. /// </summary>
  144. /// <param name="key">键</param>
  145. /// <param name="asString">是否把值作为字符串返回</param>
  146. public static T Get<T>(string key, object value, bool asString)
  147. {
  148. return (T)mc.Get(key, value, asString);
  149. }
  150. #endregion
  151.  
  152. /// <summary>
  153. /// 读取数据缓存
  154. /// </summary>
  155. /// <param name="key">键</param>
  156. public static object Get(string key)
  157. {
  158. return mc.Get(key);
  159. }
  160. /// <summary>
  161. /// 读取数据缓存
  162. /// </summary>
  163. /// <param name="key">键</param>
  164. /// <param name="hashCode">哈希码</param>
  165. public static object Get(string key, int hashCode)
  166. {
  167. return mc.Get(key, hashCode);
  168. }
  169. /// <summary>
  170. /// 读取数据缓存
  171. /// </summary>
  172. /// <param name="key">键</param>
  173. /// <param name="asString">是否把值作为字符串返回</param>
  174. public static object Get(string key, object value, bool asString)
  175. {
  176. return mc.Get(key, value, asString);
  177. }
  178. #endregion
  179.  
  180. #region 批量写(Set)
  181. /// <summary>
  182. /// 批量设置数据缓存
  183. /// </summary>
  184. /// <param name="key">键</param>
  185. /// <param name="value">值</param>
  186. public static void SetMultiple(string[] keys, object[] values)
  187. {
  188. for (int i = ; i < keys.Length; i++)
  189. {
  190. mc.Set(keys[i], values[i]);
  191. }
  192. }
  193. /// <summary>
  194. /// 批量设置数据缓存
  195. /// </summary>
  196. /// <param name="key">键</param>
  197. /// <param name="value">值</param>
  198. /// <param name="hashCode">哈希码</param>
  199. public static void SetMultiple(string[] keys, object[] values, int[] hashCodes)
  200. {
  201. for (int i = ; i < keys.Length; i++)
  202. {
  203. mc.Set(keys[i], values[i], hashCodes[i]);
  204. }
  205. }
  206. /// <summary>
  207. /// 批量设置数据缓存
  208. /// </summary>
  209. /// <param name="key">键</param>
  210. /// <param name="value">值</param>
  211. /// <param name="expiry">过期时间</param>
  212. public static void SetMultiple(string[] keys, object[] values, DateTime[] expirys)
  213. {
  214. for (int i = ; i < keys.Length; i++)
  215. {
  216. mc.Set(keys[i], values[i], expirys[i]);
  217. }
  218. }
  219. /// <summary>
  220. /// 批量设置数据缓存
  221. /// </summary>
  222. /// <param name="key">键</param>
  223. /// <param name="value">值</param>
  224. /// <param name="expiry">过期时间</param>
  225. public static void Set(string[] keys, object[] values, DateTime[] expirys, int[] hashCodes)
  226. {
  227. for (int i = ; i < keys.Length; i++)
  228. {
  229. mc.Set(keys[i], values[i], expirys[i], hashCodes[i]);
  230. }
  231. }
  232. #endregion
  233.  
  234. #region 批量读取(Multiple),返回哈希表 Hashtable
  235. /// <summary>
  236. /// 批量读取数据缓存
  237. /// </summary>
  238. /// <param name="keys">键集合</param>
  239. public static Hashtable GetMultiple(string[] keys)
  240. {
  241. return mc.GetMultiple(keys);
  242. }
  243. /// <summary>
  244. /// 批量读取数据缓存
  245. /// </summary>
  246. /// <param name="keys">键集合</param>
  247. /// <param name="hashCodes">哈希码集合</param>
  248. public static Hashtable GetMultiple(string[] keys, int[] hashCodes)
  249. {
  250. return mc.GetMultiple(keys, hashCodes);
  251. }
  252. /// <summary>
  253. /// 批量读取数据缓存
  254. /// </summary>
  255. /// <param name="keys">键集合</param>
  256. /// <param name="hashCodes">哈希码集合</param>
  257. /// <param name="asString">所有值返回字符</param>
  258. public static Hashtable GetMultiple(string[] keys, int[] hashCodes, bool asString)
  259. {
  260. return mc.GetMultiple(keys, hashCodes, asString);
  261. }
  262. #endregion
  263.  
  264. #region 批量读取(Multiple),返回对象数组object[]
  265. /// <summary>
  266. /// 批量读取数据缓存
  267. /// </summary>
  268. /// <param name="keys">键集合</param>
  269. public static object[] GetMultipleArray(string[] keys)
  270. {
  271. return mc.GetMultipleArray(keys);
  272. }
  273. /// <summary>
  274. /// 批量读取数据缓存
  275. /// </summary>
  276. /// <param name="keys">键集合</param>
  277. /// <param name="hashCodes">哈希码集合</param>
  278. public static object[] GetMultipleArray(string[] keys, int[] hashCodes)
  279. {
  280. return mc.GetMultipleArray(keys, hashCodes);
  281. }
  282. /// <summary>
  283. /// 批量读取数据缓存
  284. /// </summary>
  285. /// <param name="keys">键集合</param>
  286. /// <param name="hashCodes">哈希码集合</param>
  287. /// <param name="asString">所有值返回字符</param>
  288. public static object[] GetMultipleArray(string[] keys, int[] hashCodes, bool asString)
  289. {
  290. return mc.GetMultipleArray(keys, hashCodes, asString);
  291. }
  292. #endregion
  293.  
  294. #region 批量读取(Multiple),返回泛型集合List[T]
  295. /// <summary>
  296. /// 批量读取数据缓存
  297. /// </summary>
  298. /// <param name="keys">键集合</param>
  299. public static List<T> GetMultipleList<T>(string[] keys)
  300. {
  301. object[] obj = mc.GetMultipleArray(keys);
  302. List<T> list = new List<T>();
  303. foreach (object o in obj)
  304. {
  305. list.Add((T)o);
  306. }
  307. return list;
  308. }
  309. /// <summary>
  310. /// 批量读取数据缓存
  311. /// </summary>
  312. /// <param name="keys">键集合</param>
  313. /// <param name="hashCodes">哈希码集合</param>
  314. public static List<T> GetMultipleList<T>(string[] keys, int[] hashCodes)
  315. {
  316. object[] obj = mc.GetMultipleArray(keys, hashCodes);
  317. List<T> list = new List<T>();
  318. foreach (object o in obj)
  319. {
  320. list.Add((T)o);
  321. }
  322. return list;
  323. }
  324. /// <summary>
  325. /// 批量读取数据缓存
  326. /// </summary>
  327. /// <param name="keys">键集合</param>
  328. /// <param name="hashCodes">哈希码集合</param>
  329. /// <param name="asString">所有值返回字符</param>
  330. public static List<T> GetMultipleList<T>(string[] keys, int[] hashCodes, bool asString)
  331. {
  332. object[] obj = mc.GetMultipleArray(keys, hashCodes, asString);
  333. List<T> list = new List<T>();
  334. foreach (object o in obj)
  335. {
  336. list.Add((T)o);
  337. }
  338. return list;
  339. }
  340. #endregion
  341.  
  342. #region 替换更新(Replace)
  343. /// <summary>
  344. /// 替换更新数据缓存
  345. /// </summary>
  346. /// <param name="key">键</param>
  347. /// <param name="value">值</param>
  348. public static void Replace(string key, object value)
  349. {
  350. mc.Replace(key, value);
  351. }
  352. /// <summary>
  353. /// 替换更新数据缓存
  354. /// </summary>
  355. /// <param name="key">键</param>
  356. /// <param name="value">值</param>
  357. /// <param name="hashCode">哈希码</param>
  358. public static void Replace(string key, object value, int hashCode)
  359. {
  360. mc.Replace(key, value, hashCode);
  361. }
  362. /// <summary>
  363. /// 替换更新数据缓存
  364. /// </summary>
  365. /// <param name="key">键</param>
  366. /// <param name="value">值</param>
  367. /// <param name="expiry">过期时间</param>
  368. public static void Replace(string key, object value, DateTime expiry)
  369. {
  370. mc.Replace(key, value, expiry);
  371. }
  372. /// <summary>
  373. /// 替换更新数据缓存
  374. /// </summary>
  375. /// <param name="key">键</param>
  376. /// <param name="value">值</param>
  377. /// <param name="expiry">过期时间</param>
  378. public static void Replace(string key, object value, DateTime expiry, int hashCode)
  379. {
  380. mc.Replace(key, value, expiry, hashCode);
  381. }
  382. #endregion
  383.  
  384. #region 删除(Delete)
  385.  
  386. /// <summary>
  387. ///删除指定条件缓存
  388. /// </summary>
  389. /// <param name="key">键</param>
  390. public static bool Delete(string key)
  391. {
  392. return mc.Delete(key);
  393. }
  394. /// <summary>
  395. /// 删除指定条件缓存
  396. /// </summary>
  397. /// <param name="key">键</param>
  398. /// <param name="hashCode">哈希码</param>
  399. /// <param name="expiry">过期时间</param>
  400. public static bool Delete(string key, int hashCode, DateTime expiry)
  401. {
  402. return mc.Delete(key, hashCode, expiry);
  403. }
  404. /// <summary>
  405. /// 删除指定条件缓存
  406. /// </summary>
  407. /// <param name="key">键</param>
  408. /// <param name="expiry">过期时间</param>
  409. public static bool Delete(string key, DateTime expiry)
  410. {
  411. return mc.Delete(key, expiry);
  412. }
  413.  
  414. /// <summary>
  415. /// 移除全部缓存
  416. /// </summary>
  417. public static void RemovAllCache()
  418. {
  419. mc.FlushAll();
  420. }
  421. /// <summary>
  422. /// 移除全部缓存
  423. /// </summary>
  424. /// <param name="list">移除指定服务器缓存</param>
  425. public static void RemovAllCache(ArrayList list)
  426. {
  427. mc.FlushAll(list);
  428. }
  429. #endregion
  430.  
  431. #region 是否存在(Exists)
  432. /// <summary>
  433. /// 判断指定键的缓存是否存在
  434. /// </summary>
  435. /// <param name="key">键</param>
  436. /// <returns></returns>
  437. public static bool IsExists(string key)
  438. {
  439. return mc.KeyExists(key);
  440. }
  441. #endregion
  442.  
  443. #region 数值增减
  444.  
  445. #region 存储一个数值元素
  446. /// <summary>
  447. /// 存储一个数值元素
  448. /// </summary>
  449. /// <param name="key">键</param>
  450. /// <returns></returns>
  451. public static bool StoreCounter(string key, long counter)
  452. {
  453. return mc.StoreCounter(key, counter);
  454. }
  455. /// <summary>
  456. /// 存储一个数值元素
  457. /// </summary>
  458. /// <param name="key">键</param>
  459. /// <param name="inc">增长幅度</param>
  460. /// <param name="hashCode">哈希码</param>
  461. /// <returns></returns>
  462. public static bool StoreCounter(string key, long counter, int hashCode)
  463. {
  464. return mc.StoreCounter(key, counter, hashCode);
  465. }
  466. #endregion
  467.  
  468. #region 获取一个数值元素
  469. /// <summary>
  470. /// 获取一个数值元素
  471. /// </summary>
  472. /// <param name="key">键</param>
  473. /// <returns></returns>
  474. public static long GetCounter(string key)
  475. {
  476. return mc.GetCounter(key);
  477. }
  478. /// <summary>
  479. /// 获取一个数值元素
  480. /// </summary>
  481. /// <param name="key">键</param>
  482. /// <param name="hashCode">哈希码</param>
  483. /// <returns></returns>
  484. public static long GetCounter(string key, int hashCode)
  485. {
  486. return mc.GetCounter(key, hashCode);
  487. }
  488. #endregion
  489.  
  490. #region 增加一个数值元素的值(Increment)
  491. /// <summary>
  492. /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理
  493. /// </summary>
  494. /// <param name="key">键</param>
  495. /// <returns></returns>
  496. public static long Increment(string key)
  497. {
  498. return mc.Increment(key);
  499. }
  500. /// <summary>
  501. /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理
  502. /// </summary>
  503. /// <param name="key">键</param>
  504. /// <param name="inc">增长幅度</param>
  505. /// <returns></returns>
  506. public static long Increment(string key, long inc)
  507. {
  508. return mc.Increment(key, inc);
  509. }
  510. /// <summary>
  511. /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理
  512. /// </summary>
  513. /// <param name="key">键</param>
  514. /// <param name="inc">增长幅度</param>
  515. /// <param name="hashCode">哈希码</param>
  516. /// <returns></returns>
  517. public static long Increment(string key, long inc, int hashCode)
  518. {
  519. return mc.Increment(key, inc, hashCode);
  520. }
  521. #endregion
  522.  
  523. #region 减小一个数值元素的值(Decrement)
  524. /// <summary>
  525. /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0
  526. /// </summary>
  527. /// <param name="key">键</param>
  528. /// <returns></returns>
  529. public static long Decrement(string key)
  530. {
  531. return mc.Decrement(key);
  532. }
  533. /// <summary>
  534. /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0
  535. /// </summary>
  536. /// <param name="key">键</param>
  537. /// <param name="inc">增长幅度</param>
  538. /// <returns></returns>
  539. public static long Decrement(string key, long inc)
  540. {
  541. return mc.Decrement(key, inc);
  542. }
  543. /// <summary>
  544. /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0
  545. /// </summary>
  546. /// <param name="key">键</param>
  547. /// <param name="inc">增长幅度</param>
  548. /// <param name="hashCode">哈希码</param>
  549. /// <returns></returns>
  550. public static long Decrement(string key, long inc, int hashCode)
  551. {
  552. return mc.Decrement(key, inc, hashCode);
  553. }
  554. #endregion
  555.  
  556. #endregion
  557.  
  558. #endregion
  559.  
  560. #region 指定服务器
  561.  
  562. #region 获取(Get)
  563.  
  564. /// <summary>
  565. /// 从指定服务器获取
  566. /// </summary>
  567. /// <param name="server">服务器,Svr1,Svr2</param>
  568. /// <param name="key">键</param>
  569. /// <returns></returns>
  570. public static object GetFrom(string server, string key)
  571. {
  572. MemcachedClient client = GetClient(server);
  573. client.PoolName = server;
  574. return client.Get(key);
  575. }
  576. /// <summary>
  577. /// 从指定服务器获取
  578. /// </summary>
  579. /// <param name="server">服务器,Svr1,Svr2</param>
  580. /// <param name="key">键</param>
  581. /// <param name="hashCode">哈希码</param>
  582. public static object GetFrom(string server, string key, int hashCode)
  583. {
  584. MemcachedClient client = GetClient(server);
  585. client.PoolName = server;
  586. return client.Get(key, hashCode);
  587. }
  588. /// <summary>
  589. /// 从指定服务器获取
  590. /// </summary>
  591. /// <param name="server">服务器,Svr1,Svr2</param>
  592. /// <param name="key">键</param>
  593. /// <param name="asString">是否把值作为字符串返回</param>
  594. public static object GetFrom(string server, string key, object value, bool asString)
  595. {
  596. MemcachedClient client = GetClient(server);
  597. client.PoolName = server;
  598. return client.Get(key, value, asString);
  599. }
  600. #endregion
  601.  
  602. #region 写入(Set)
  603. /// <summary>
  604. /// 设置数据缓存
  605. /// </summary>
  606. /// <param name="server">服务器,格式为Svr1,Svr2,Svr3,对应配置文件host</param>
  607. /// <param name="key">键</param>
  608. /// <param name="value">值</param>
  609. public static void SetTo(string server, string key, object value)
  610. {
  611. MemcachedClient client = GetClient(server);
  612. client.PoolName = server;
  613. client.Set(key, value);
  614. }
  615. /// <summary>
  616. /// 设置数据缓存
  617. /// </summary>
  618. /// <param name="server">服务器,格式为Svr1,Svr2,Svr3,对应配置文件host</param>
  619. /// <param name="key">键</param>
  620. /// <param name="value">值</param>
  621. /// <param name="hashCode">哈希码</param>
  622. public static void SetTo(string server, string key, object value, int hashCode)
  623. {
  624. MemcachedClient client = GetClient(server);
  625. client.PoolName = server;
  626. client.Set(key, value, hashCode);
  627. }
  628. /// <summary>
  629. /// 设置数据缓存
  630. /// </summary>
  631. /// <param name="server">服务器,格式为Svr1,Svr2,Svr3,对应配置文件host</param>
  632. /// <param name="key">键</param>
  633. /// <param name="value">值</param>
  634. /// <param name="expiry">过期时间</param>
  635. public static void SetTo(string server, string key, object value, DateTime expiry)
  636. {
  637. MemcachedClient client = GetClient(server);
  638. client.PoolName = server;
  639. client.Set(key, value, expiry);
  640. }
  641. /// <summary>
  642. /// 设置数据缓存
  643. /// </summary>
  644. /// <param name="server">服务器,格式为Svr1,Svr2,Svr3,对应配置文件host</param>
  645. /// <param name="key">键</param>
  646. /// <param name="value">值</param>
  647. /// <param name="expiry">过期时间</param>
  648. public static void SetTo(string server, string key, object value, DateTime expiry, int hashCode)
  649. {
  650. MemcachedClient client = GetClient(server);
  651. client.PoolName = server;
  652. client.Set(key, value, expiry, hashCode);
  653. }
  654. #endregion
  655.  
  656. #region 批量写(Set)
  657. /// <summary>
  658. /// 批量设置数据缓存
  659. /// </summary>
  660. /// <param name="key">键</param>
  661. /// <param name="value">值</param>
  662. public static void SetMultipleTo(string server, string[] keys, object[] values)
  663. {
  664. MemcachedClient client = GetClient(server);
  665. client.PoolName = server;
  666. for (int i = ; i < keys.Length; i++)
  667. {
  668. client.Set(keys[i], values[i]);
  669. }
  670. }
  671. /// <summary>
  672. /// 批量设置数据缓存
  673. /// </summary>
  674. /// <param name="key">键</param>
  675. /// <param name="value">值</param>
  676. /// <param name="hashCode">哈希码</param>
  677. public static void SetMultipleTo(string server, string[] keys, object[] values, int[] hashCodes)
  678. {
  679. MemcachedClient client = GetClient(server);
  680. client.PoolName = server;
  681. for (int i = ; i < keys.Length; i++)
  682. {
  683. client.Set(keys[i], values[i], hashCodes[i]);
  684. }
  685. }
  686. /// <summary>
  687. /// 批量设置数据缓存
  688. /// </summary>
  689. /// <param name="key">键</param>
  690. /// <param name="value">值</param>
  691. /// <param name="expiry">过期时间</param>
  692. public static void SetMultipleTo(string server, string[] keys, object[] values, DateTime[] expirys)
  693. {
  694. MemcachedClient client = GetClient(server);
  695. client.PoolName = server;
  696. for (int i = ; i < keys.Length; i++)
  697. {
  698. client.Set(keys[i], values[i], expirys[i]);
  699. }
  700. }
  701. /// <summary>
  702. /// 批量设置数据缓存
  703. /// </summary>
  704. /// <param name="key">键</param>
  705. /// <param name="value">值</param>
  706. /// <param name="expiry">过期时间</param>
  707. public static void SetMultipleTo(string server, string[] keys, object[] values, DateTime[] expirys, int[] hashCodes)
  708. {
  709. MemcachedClient client = GetClient(server);
  710. client.PoolName = server;
  711. for (int i = ; i < keys.Length; i++)
  712. {
  713. client.Set(keys[i], values[i], expirys[i], hashCodes[i]);
  714. }
  715. }
  716. #endregion
  717.  
  718. #region 批量读取(Multiple),返回哈希表 Hashtable
  719. /// <summary>
  720. /// 批量读取数据缓存
  721. /// </summary>
  722. /// <param name="keys">键集合</param>
  723. public static Hashtable GetMultipleFrom(string server, string[] keys)
  724. {
  725. MemcachedClient client = GetClient(server);
  726. client.PoolName = server;
  727. return client.GetMultiple(keys);
  728. }
  729. /// <summary>
  730. /// 批量读取数据缓存
  731. /// </summary>
  732. /// <param name="keys">键集合</param>
  733. /// <param name="hashCodes">哈希码集合</param>
  734. public static Hashtable GetMultipleFrom(string server, string[] keys, int[] hashCodes)
  735. {
  736. MemcachedClient client = GetClient(server);
  737. client.PoolName = server;
  738. return client.GetMultiple(keys, hashCodes);
  739. }
  740. /// <summary>
  741. /// 批量读取数据缓存
  742. /// </summary>
  743. /// <param name="keys">键集合</param>
  744. /// <param name="hashCodes">哈希码集合</param>
  745. /// <param name="asString">所有值返回字符</param>
  746. public static Hashtable GetMultipleFrom(string server, string[] keys, int[] hashCodes, bool asString)
  747. {
  748. MemcachedClient client = GetClient(server);
  749. client.PoolName = server;
  750. return client.GetMultiple(keys, hashCodes, asString);
  751. }
  752. #endregion
  753.  
  754. #region 批量读取(Multiple),返回对象数组object[]
  755. /// <summary>
  756. /// 批量读取数据缓存
  757. /// </summary>
  758. /// <param name="keys">键集合</param>
  759. public static object[] GetMultipleArrayFrom(string server, string[] keys)
  760. {
  761. MemcachedClient client = GetClient(server);
  762. client.PoolName = server;
  763. return client.GetMultipleArray(keys);
  764. }
  765. /// <summary>
  766. /// 批量读取数据缓存
  767. /// </summary>
  768. /// <param name="keys">键集合</param>
  769. /// <param name="hashCodes">哈希码集合</param>
  770. public static object[] GetMultipleArrayFrom(string server, string[] keys, int[] hashCodes)
  771. {
  772. MemcachedClient client = GetClient(server);
  773. client.PoolName = server;
  774. return client.GetMultipleArray(keys, hashCodes);
  775. }
  776. /// <summary>
  777. /// 批量读取数据缓存
  778. /// </summary>
  779. /// <param name="keys">键集合</param>
  780. /// <param name="hashCodes">哈希码集合</param>
  781. /// <param name="asString">所有值返回字符</param>
  782. public static object[] GetMultipleArrayFrom(string server, string[] keys, int[] hashCodes, bool asString)
  783. {
  784. MemcachedClient client = GetClient(server);
  785. client.PoolName = server;
  786. return client.GetMultipleArray(keys, hashCodes, asString);
  787. }
  788. #endregion
  789.  
  790. #region 批量读取(Multiple),返回泛型集合List[T]
  791. /// <summary>
  792. /// 批量读取数据缓存
  793. /// </summary>
  794. /// <param name="keys">键集合</param>
  795. public static List<T> GetMultipleListFrom<T>(string server, string[] keys)
  796. {
  797. MemcachedClient client = GetClient(server);
  798. client.PoolName = server;
  799. object[] obj = client.GetMultipleArray(keys);
  800. List<T> list = new List<T>();
  801. foreach (object o in obj)
  802. {
  803. list.Add((T)o);
  804. }
  805. return list;
  806. }
  807. /// <summary>
  808. /// 批量读取数据缓存
  809. /// </summary>
  810. /// <param name="keys">键集合</param>
  811. /// <param name="hashCodes">哈希码集合</param>
  812. public static List<T> GetMultipleListFrom<T>(string server, string[] keys, int[] hashCodes)
  813. {
  814. MemcachedClient client = GetClient(server);
  815. client.PoolName = server;
  816. object[] obj = client.GetMultipleArray(keys, hashCodes);
  817. List<T> list = new List<T>();
  818. foreach (object o in obj)
  819. {
  820. list.Add((T)o);
  821. }
  822. return list;
  823. }
  824. /// <summary>
  825. /// 批量读取数据缓存
  826. /// </summary>
  827. /// <param name="keys">键集合</param>
  828. /// <param name="hashCodes">哈希码集合</param>
  829. /// <param name="asString">所有值返回字符</param>
  830. public static List<T> GetMultipleListFrom<T>(string server, string[] keys, int[] hashCodes, bool asString)
  831. {
  832. MemcachedClient client = GetClient(server);
  833. client.PoolName = server;
  834. object[] obj = client.GetMultipleArray(keys, hashCodes, asString);
  835. List<T> list = new List<T>();
  836. foreach (object o in obj)
  837. {
  838. list.Add((T)o);
  839. }
  840. return list;
  841. }
  842. #endregion
  843.  
  844. #region 替换更新(Replace)
  845. /// <summary>
  846. /// 替换更新数据缓存
  847. /// </summary>
  848. /// <param name="key">键</param>
  849. /// <param name="value">值</param>
  850. public static void ReplaceFrom(string server, string key, object value)
  851. {
  852. MemcachedClient client = GetClient(server);
  853. client.PoolName = server;
  854. client.Replace(key, value);
  855. }
  856. /// <summary>
  857. /// 替换更新数据缓存
  858. /// </summary>
  859. /// <param name="key">键</param>
  860. /// <param name="value">值</param>
  861. /// <param name="hashCode">哈希码</param>
  862. public static void ReplaceFrom(string server, string key, object value, int hashCode)
  863. {
  864. MemcachedClient client = GetClient(server);
  865. client.PoolName = server;
  866. client.Replace(key, value, hashCode);
  867. }
  868. /// <summary>
  869. /// 替换更新数据缓存
  870. /// </summary>
  871. /// <param name="key">键</param>
  872. /// <param name="value">值</param>
  873. /// <param name="expiry">过期时间</param>
  874. public static void ReplaceFrom(string server, string key, object value, DateTime expiry)
  875. {
  876. MemcachedClient client = GetClient(server);
  877. client.PoolName = server;
  878. client.Replace(key, value, expiry);
  879. }
  880. /// <summary>
  881. /// 替换更新数据缓存
  882. /// </summary>
  883. /// <param name="key">键</param>
  884. /// <param name="value">值</param>
  885. /// <param name="expiry">过期时间</param>
  886. public static void ReplaceFrom(string server, string key, object value, DateTime expiry, int hashCode)
  887. {
  888. MemcachedClient client = GetClient(server);
  889. client.PoolName = server;
  890. client.Replace(key, value, expiry, hashCode);
  891. }
  892. #endregion
  893.  
  894. #region 删除(Delete)
  895.  
  896. /// <summary>
  897. ///删除指定条件缓存
  898. /// </summary>
  899. /// <param name="key">键</param>
  900. public static bool DeleteFrom(string server, string key)
  901. {
  902. MemcachedClient client = GetClient(server);
  903. client.PoolName = server;
  904. return client.Delete(key);
  905. }
  906. /// <summary>
  907. /// 删除指定条件缓存
  908. /// </summary>
  909. /// <param name="key">键</param>
  910. /// <param name="hashCode">哈希码</param>
  911. /// <param name="expiry">过期时间</param>
  912. public static bool DeleteFrom(string server, string key, int hashCode, DateTime expiry)
  913. {
  914. MemcachedClient client = GetClient(server);
  915. client.PoolName = server;
  916. return client.Delete(key, hashCode, expiry);
  917. }
  918. /// <summary>
  919. /// 删除指定条件缓存
  920. /// </summary>
  921. /// <param name="key">键</param>
  922. /// <param name="expiry">过期时间</param>
  923. public static bool DeleteFrom(string server, string key, DateTime expiry)
  924. {
  925. MemcachedClient client = GetClient(server);
  926. client.PoolName = server;
  927. return client.Delete(key, expiry);
  928. }
  929.  
  930. /// <summary>
  931. /// 移除全部缓存
  932. /// </summary>
  933. public static void RemovAllCacheFrom(string server)
  934. {
  935. MemcachedClient client = GetClient(server);
  936. client.PoolName = server;
  937. client.FlushAll();
  938. }
  939. /// <summary>
  940. /// 移除全部缓存
  941. /// </summary>
  942. /// <param name="list">移除指定服务器缓存</param>
  943. public static void RemovAllCacheFrom(string server, ArrayList list)
  944. {
  945. MemcachedClient client = GetClient(server);
  946. client.PoolName = server;
  947. client.FlushAll(list);
  948. }
  949. #endregion
  950.  
  951. #region 是否存在(Exists)
  952. /// <summary>
  953. /// 判断指定键的缓存是否存在
  954. /// </summary>
  955. /// <param name="key">键</param>
  956. /// <returns></returns>
  957. public static bool IsExists(string server, string key)
  958. {
  959. MemcachedClient client = GetClient(server);
  960. client.PoolName = server;
  961. return client.KeyExists(key);
  962. }
  963. #endregion
  964.  
  965. #region 数值增减
  966.  
  967. #region 存储一个数值元素
  968. /// <summary>
  969. /// 存储一个数值元素
  970. /// </summary>
  971. /// <param name="key">键</param>
  972. /// <returns></returns>
  973. public static bool StoreCounterTo(string server, string key, long counter)
  974. {
  975. MemcachedClient client = GetClient(server);
  976. client.PoolName = server;
  977. return client.StoreCounter(key, counter);
  978. }
  979. /// <summary>
  980. /// 存储一个数值元素
  981. /// </summary>
  982. /// <param name="key">键</param>
  983. /// <param name="inc">增长幅度</param>
  984. /// <param name="hashCode">哈希码</param>
  985. /// <returns></returns>
  986. public static bool StoreCounterTo(string server, string key, long counter, int hashCode)
  987. {
  988. MemcachedClient client = GetClient(server);
  989. client.PoolName = server;
  990. return client.StoreCounter(key, counter, hashCode);
  991. }
  992. #endregion
  993.  
  994. #region 获取一个数值元素
  995. /// <summary>
  996. /// 获取一个数值元素
  997. /// </summary>
  998. /// <param name="key">键</param>
  999. /// <returns></returns>
  1000. public static long GetCounterFrom(string server, string key)
  1001. {
  1002. MemcachedClient client = GetClient(server);
  1003. client.PoolName = server;
  1004. return client.GetCounter(key);
  1005. }
  1006. /// <summary>
  1007. /// 获取一个数值元素
  1008. /// </summary>
  1009. /// <param name="key">键</param>
  1010. /// <param name="hashCode">哈希码</param>
  1011. /// <returns></returns>
  1012. public static long GetCounterFrom(string server, string key, int hashCode)
  1013. {
  1014. MemcachedClient client = GetClient(server);
  1015. client.PoolName = server;
  1016. return client.GetCounter(key, hashCode);
  1017. }
  1018. #endregion
  1019.  
  1020. #region 增加一个数值元素的值(Increment)
  1021. /// <summary>
  1022. /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理
  1023. /// </summary>
  1024. /// <param name="key">键</param>
  1025. /// <returns></returns>
  1026. public static long IncrementTo(string server, string key)
  1027. {
  1028. MemcachedClient client = GetClient(server);
  1029. client.PoolName = server;
  1030. return client.Increment(key);
  1031. }
  1032. /// <summary>
  1033. /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理
  1034. /// </summary>
  1035. /// <param name="key">键</param>
  1036. /// <param name="inc">增长幅度</param>
  1037. /// <returns></returns>
  1038. public static long IncrementTo(string server, string key, long inc)
  1039. {
  1040. MemcachedClient client = GetClient(server);
  1041. client.PoolName = server;
  1042. return client.Increment(key, inc);
  1043. }
  1044. /// <summary>
  1045. /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理
  1046. /// </summary>
  1047. /// <param name="key">键</param>
  1048. /// <param name="inc">增长幅度</param>
  1049. /// <param name="hashCode">哈希码</param>
  1050. /// <returns></returns>
  1051. public static long IncrementTo(string server, string key, long inc, int hashCode)
  1052. {
  1053. MemcachedClient client = GetClient(server);
  1054. client.PoolName = server;
  1055. return client.Increment(key, inc, hashCode);
  1056. }
  1057. #endregion
  1058.  
  1059. #region 减小一个数值元素的值(Decrement)
  1060. /// <summary>
  1061. /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0
  1062. /// </summary>
  1063. /// <param name="key">键</param>
  1064. /// <returns></returns>
  1065. public static long DecrementFrom(string server, string key)
  1066. {
  1067. MemcachedClient client = GetClient(server);
  1068. client.PoolName = server;
  1069. return client.Decrement(key);
  1070. }
  1071. /// <summary>
  1072. /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0
  1073. /// </summary>
  1074. /// <param name="key">键</param>
  1075. /// <param name="inc">增长幅度</param>
  1076. /// <returns></returns>
  1077. public static long DecrementFrom(string server, string key, long inc)
  1078. {
  1079. MemcachedClient client = GetClient(server);
  1080. client.PoolName = server;
  1081. return client.Decrement(key, inc);
  1082. }
  1083. /// <summary>
  1084. /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0
  1085. /// </summary>
  1086. /// <param name="key">键</param>
  1087. /// <param name="inc">增长幅度</param>
  1088. /// <param name="hashCode">哈希码</param>
  1089. /// <returns></returns>
  1090. public static long DecrementFrom(string server, string key, long inc, int hashCode)
  1091. {
  1092. MemcachedClient client = GetClient(server);
  1093. client.PoolName = server;
  1094. return client.Decrement(key, inc, hashCode);
  1095. }
  1096. #endregion
  1097.  
  1098. #endregion
  1099.  
  1100. #endregion
  1101. }
  1102.  
  1103. //Singleton
  1104.  
  1105. /// <summary>
  1106. /// 泛型单例模式
  1107. /// </summary>
  1108. /// <typeparam name="T"></typeparam>
  1109. public class Singleton<T> where T : class
  1110. {
  1111. private static object locobj = new object();
  1112. private static T _current = null;
  1113.  
  1114. public static T GetInstance()
  1115. {
  1116. //双重锁定,保证多线程时安全。
  1117. if (_current == null)
  1118. {
  1119. lock (locobj)
  1120. {
  1121. if (_current == null)
  1122. {
  1123. //instance = new T();
  1124. //需要非公共的无参构造函数,不能使用new T() ,new不支持非公共的无参构造函数
  1125. _current = Activator.CreateInstance(typeof(T), true) as T;//第二个参数防止异常:“没有为该对象定义无参数的构造函数。”
  1126. }
  1127.  
  1128. }
  1129. }
  1130. return _current;
  1131.  
  1132. }
  1133.  
  1134. }

2、测试类库

  1. static void Main(string[] args)
  2. {
  3. #region
  4. //// Memcached服务器列表
  5. //// 如果有多台服务器,则以逗号分隔,例如:"192.168.80.10:11211","192.168.80.11:11211"
  6. //string[] serverList = ConfigurationManager.AppSettings["memservers"].Split(',');
  7. //// 初始化SocketIO池
  8. //string poolName = "MyPool";
  9. //SockIOPool sockIOPool = SockIOPool.GetInstance(poolName);
  10. //// 添加服务器列表
  11. //sockIOPool.SetServers(serverList);
  12. //// 设置连接池初始数目
  13. //sockIOPool.InitConnections = 3;
  14. //// 设置连接池最小连接数目
  15. //sockIOPool.MinConnections = 3;
  16. //// 设置连接池最大连接数目
  17. //sockIOPool.MaxConnections = 5;
  18. //// 设置连接的套接字超时时间(单位:毫秒)
  19. //sockIOPool.SocketConnectTimeout = 1000;
  20. //// 设置套接字超时时间(单位:毫秒)
  21. //sockIOPool.SocketTimeout = 3000;
  22. //// 设置维护线程运行的睡眠时间:如果设置为0,那么维护线程将不会启动
  23. //sockIOPool.MaintenanceSleep = 30;
  24. //// 设置SockIO池的故障标志
  25. //sockIOPool.Failover = true;
  26. //// 是否用nagle算法启动
  27. //sockIOPool.Nagle = false;
  28. //// 正式初始化容器
  29. //sockIOPool.Initialize();
  30.  
  31. //// 获取Memcached客户端实例
  32. //MemcachedClient memClient = new MemcachedClient();
  33. //// 指定客户端访问的SockIO池
  34. //memClient.PoolName = poolName;
  35. //// 是否启用压缩数据:如果启用了压缩,数据压缩长于门槛的数据将被储存在压缩的形式
  36. //memClient.EnableCompression = false;
  37.  
  38. //Console.WriteLine("----------------------------测试开始----------------------------");
  39. //// 01.简单的添加与读取操作
  40. //memClient.Set("test1", "edisonchou");
  41. //Console.WriteLine("test1:{0}", memClient.Get("test1"));
  42. //// 02.先添加后修改再读取操作
  43. //memClient.Set("test2", "jacky");
  44. //Console.WriteLine("test2:{0}", memClient.Get("test2"));
  45. //memClient.Set("test2", "edwin");
  46. //Console.WriteLine("test2:{0}", memClient.Get("test2"));
  47. //memClient.Replace("test2", "lousie");
  48. //Console.WriteLine("test2:{0}", memClient.Get("test2"));
  49. //// 03.判断Key值是否存在
  50. //if (memClient.KeyExists("test2"))
  51. //{
  52. // Console.WriteLine("Key:test2 is existed");
  53. //}
  54. //// 04.删除指定Key值的数据
  55. //memClient.Add("test3", "memcached");
  56. //Console.WriteLine("test3:{0}", memClient.Get("test3"));
  57. //memClient.Delete("test3");
  58. //if (!memClient.KeyExists("test3"))
  59. //{
  60. // Console.WriteLine("Key:test3 is not existed");
  61. //}
  62. //// 05.设置数据过期时间:5秒后过期
  63. //memClient.Add("test4", "expired", DateTime.Now.AddSeconds(5));
  64. //Console.WriteLine("test4:{0}", memClient.Get("test4"));
  65. //Console.WriteLine("Please waiting the sleeping time");
  66. //System.Threading.Thread.Sleep(6000);
  67. //if (!memClient.KeyExists("test4"))
  68. //{
  69. // Console.WriteLine("test4 is expired");
  70. //}
  71. //Console.WriteLine("----------------------------测试完成----------------------------");
  72.  
  73. //// 关闭SockIO池
  74. //sockIOPool.Shutdown();
  75.  
  76. #endregion
  77.  
  78. MemcachedClient client = MemcachedHelper.GetClient("memservers");
  79.  
  80. client.Set("name", "mopheify", DateTime.Now.AddMinutes());
  81.  
  82. var obj= client.Get("name");
  83. Console.WriteLine(obj==null?"Not Hid":obj.ToString());
  84.  
  85. Console.ReadKey();
  86. }

3、简单封装

  1. public interface ICache
  2. {
  3.  
  4. bool ContainKey(string argKey);
  5. bool Set(string argKey, object argValue);
  6. bool Set(string argKey, object argValue, DateTime argDateExpiration);
  7. bool Replace(string argKey, object argValue);
  8. bool Replace(string argKey, object argValue, DateTime argDateExpiration);
  9. object Get(string argKey);
  10. bool Remove(string argKey);
  11. bool Remove(string argKey, DateTime argDateExpiration);
  12. bool Remove();
  13. bool Remove(ArrayList servers);
  14.  
  15. }
  16.  
  17. public interface ICache
  18. {
  19.  
  20. bool ContainKey(string argKey);
  21. bool Set(string argKey, object argValue);
  22. bool Set(string argKey, object argValue, DateTime argDateExpiration);
  23. bool Replace(string argKey, object argValue);
  24. bool Replace(string argKey, object argValue, DateTime argDateExpiration);
  25. object Get(string argKey);
  26. bool Remove(string argKey);
  27. bool Remove(string argKey, DateTime argDateExpiration);
  28. bool Remove();
  29. bool Remove(ArrayList servers);
  30.  
  31. }
  32. 接口实现代码
  33.  
  34. public class MemCachedHelper2 : ICache
  35. {
  36. private static object lockobj = new object();
  37. private static MemcachedClient memClient = null;
  38.  
  39. public MemCachedHelper2(string[] serverList, string poolName = "myPool")
  40. {
  41. // 初始化SocketIO池
  42. SockIOPool sockIOPool = SockIOPool.GetInstance(poolName);
  43. // 添加服务器列表
  44. sockIOPool.SetServers(serverList);
  45. // 设置连接池初始数目
  46. sockIOPool.InitConnections = ;
  47. // 设置连接池最小连接数目
  48. sockIOPool.MinConnections = ;
  49. // 设置连接池最大连接数目
  50. sockIOPool.MaxConnections = ;
  51. // 设置连接的套接字超时时间(单位:毫秒)
  52. sockIOPool.SocketConnectTimeout = ;
  53. // 设置套接字超时时间(单位:毫秒)
  54. sockIOPool.SocketTimeout = ;
  55. // 设置维护线程运行的睡眠时间:如果设置为0,那么维护线程将不会启动
  56. sockIOPool.MaintenanceSleep = ;
  57. // 设置SockIO池的故障标志
  58. sockIOPool.Failover = true;
  59. // 是否用nagle算法启动
  60. sockIOPool.Nagle = false;
  61. // 正式初始化容器
  62. sockIOPool.Initialize();
  63. //单例生成客户端
  64. if (memClient == null)
  65. {
  66. lock (lockobj)
  67. {
  68. if (memClient == null)
  69. {
  70. memClient = new MemcachedClient();
  71. memClient.PoolName = poolName;
  72. }
  73. }
  74. }
  75.  
  76. }
  77.  
  78.      //
  79. public static MemcachedClient GetInstance()
  80. {
  81. return memClient;
  82. }
  83.  
  84. /// <summary>
  85. /// 值是否存在
  86. /// </summary>
  87. /// <param name="argKey"></param>
  88. /// <returns></returns>
  89. public bool ContainKey(string argKey)
  90. {
  91. return memClient.Get(argKey) == null ? false : true;
  92. }
  93.  
  94. public bool Set(string argKey, object argValue)
  95. {
  96. return memClient.Set(argKey, argValue);
  97. }
  98.  
  99. public bool Set(string argKey, object argValue, DateTime argDateExpiration)
  100. {
  101. return memClient.Set(argKey, argValue, argDateExpiration);
  102. }
  103.  
  104. public bool Replace(string argKey, object argValue)
  105. {
  106. return memClient.Replace(argKey, argValue);
  107. }
  108.  
  109. public bool Replace(string argKey, object argValue, DateTime argDateExpiration)
  110. {
  111. return memClient.Replace(argKey, argValue, argDateExpiration);
  112. }
  113.  
  114. public object Get(string argKey)
  115. {
  116. return memClient.Get(argKey);
  117. }
  118.  
  119. public bool Remove(string argKey)
  120. {
  121. return memClient.Delete(argKey);
  122. }
  123.  
  124. public bool Remove(string argKey, DateTime argDateExpiration)
  125. {
  126. return memClient.Delete(argKey, argDateExpiration);
  127. }
  128.  
  129. public bool Remove()
  130. {
  131. return memClient.FlushAll();
  132. }
  133.  
  134. public bool Remove(System.Collections.ArrayList servers)
  135. {
  136. return memClient.FlushAll(servers);
  137. }
  138. }

参考资料

周旭龙

张龙豪

NLISEVERYTHINGY

suger

.net 使用Memcached的更多相关文章

  1. 支持 .NET Core 的 Memcached 客户端 EnyimMemcachedCore

    1. 介绍 EnyimMemcachedCore 是一个支持 .NET Core 的 Memcached 客户端,是从 EnyimMemcached 迁移至 .NET Core的,源代码托管在 Git ...

  2. Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作

    一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...

  3. ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存

    ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存 part 1:给我点时间,允许我感慨一下2016年 正好有时间,总结一下最近使用的一些技术,也算是为2016年画上一个完 ...

  4. 缓存、队列(Memcached、redis、RabbitMQ)

    本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...

  5. 企业做数据缓存是使用Memcached还是选Redis?

    企业是使用Memcached还是选Redis? 在构建一款现代且由数据库驱动的Web应用程序并希望使其拥有更为出色的性能表现时,这个问题总会时不时出现.并给每一位开发人员带来困扰.在考虑对应用程序的性 ...

  6. NoSql1 在Linux(CentOS)上安装memcached及使用

    前言:       今天是初五,生活基本要从过年的节奏中回归到正常的生活了,所以想想也该想想与工作有关的事情了.我之前在工作中会经常使用memcached和redis,但是自己一直没有时间系统的好好看 ...

  7. Memcached简介

    在Web服务开发中,服务端缓存是服务实现中所常常采用的一种提高服务性能的方法.其通过记录某部分计算结果来尝试避免再次执行得到该结果所需要的复杂计算,从而提高了服务的运行效率. 除了能够提高服务的运行效 ...

  8. Linux 服务器 安装 memcached

    linux centos 一.memcached的安装 1.下载 memcached-1.4.33.tar.gz.libevent-2.0.22-stable.tar.gz 安装 memcached ...

  9. Memcached和Redis比较

    一.存储 Memcached基本只支持简单的key-value存储方式.Redis除key-value之外,还支持list,set,sorted set,hash等数据结构:Redis支持数据的备份, ...

  10. 搭建LNAMP环境(七)- PHP7源码安装Memcached和Memcache拓展

    上一篇:搭建LNAMP环境(六)- PHP7源码安装MongoDB和MongoDB拓展 一.安装Memcached 1.yum安装libevent事件触发管理器 yum -y install libe ...

随机推荐

  1. Python中使用Beautiful Soup库的超详细教程

    [参考文献] http://www.jb51.net/article/65287.htm

  2. zabbix监控java内存的脚本

    #!/bin/bash # 截取java的pid号 java_pid=`netstat -lnpt |grep |awk -F '/' '{print $1'}` # 截取$jstat命令的位置 js ...

  3. 商品录入功能v1.0【持续优化中...】

    # 录入商品 def goods_record(): print("欢迎使用铜锣辉的购物商城[商品管理][录入商品]".center(30, "*")) whi ...

  4. doors dxl 遍历object 查找

    Module m = current; //m = edit(“xxx”) Object o for o in m do { string sht = o.”shtName” Buffer bf=cr ...

  5. phpSpreadSheet 中 使用的 一些坑

    如果是upupw,它 做了 安全限制...将 上传目录 写成 uploadfiles 等 才能 写进去.. 文件路径 也不要有 中文..很有可以能 下载时 找不到路径....这个太坑...

  6. Canvas 动态小球重叠效果

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. nginx FastCGI模块配置

    这个模块允许nginx同FastCGI协同工作,并且控制哪些参数将被安全传递. location / { fastcgi_pass localhost:9000;# 或者http://ip:9000; ...

  8. php5 编译安装

    #!/bin/bash######################################## File Name: php.sh# Version: V1.0# Author: sun yu ...

  9. Nginx配置SSL报错 nginx: [emerg] unknown directive "ssl"

    Nginx配置SSL报错 nginx: [emerg] unknown directive "ssl"     出现如图所示错误,处理办法如下 去nginx解压目录下执行 ./co ...

  10. 关于WampServer一些配置修改

    1.解决WAMP mysql中文乱码问题(在mysql的my.ini文件中) 1).找到client字段并添加:default-character-set=utf8 2).找到mysql字段并添加: ...