.net 基础篇-分布式缓存MemCache

1,ADO.net 中自带的Cache 缓存,是存放在IIS Web 服务器上,导致服务器上 占用的内存空间会越来越大。(只能 单机引用缓存数据)

     MemCache: “高性能分布式缓存”  --可以把缓存的数据另外放入其他单独的服务器上,减少数据库负荷,提示访问速度。(多客户端共享缓存

     MemCache 是将缓存中的数据存入服务器的内存中,缺点是: 没提供容灾等功能。(MemCache 可以解决Session 数据共享的问题)

2,Windows下使用Memcache 方法: 

   2.1 下载 Memcache: http://code.jellycan.com/Memcache/

   2.2  将服务程序拷贝到一个磁盘上的目录:

         安装服务:cmd→Memcached.exe -d install 打开服务监控窗口可以查看服务是否启动。

         启动服务:cmd→Memcached.exe -d start(restart重启,stop关闭服务)

         检查服务是否启动:连接到Memcache控制台:telnet ServerIP 11211 输入命令:stats检查当前服务状态。

         卸载服务:Memcached.exe -d uninstall(先关闭服务)

   2.3 添加程序集引用  http://sourceforge.net/projects/memcacheddotnet/

         

   2.4 在项目中添加代码:

         String[] serverlist = { "192.168.1.100:11211", "192.168.1.101:11211" };

          //初始化MemCache 的连接池。

         SockIOPool pool =  SockIOPool.GetInstance("test");

         pool.SetServers(serverlist);

         pool.Initialize();

         mc = new MemcacheClient();

         mc.PoolName = "test";

         mc.EnableCompression = false;

         pool.Shutdown();//关闭连接池

3,MemCache 基本原理:

   Socket 服务器端

   数据:键值对存储

   内存处理的算法: 本质就是一个大的哈希表。key最大长度是255个字符。

   内存模型:Memcache预先将可支配的内存空间进行分区 (Slab),每个分区里再分成多个块(Chunk)大小1MB,但同一个分区里:块的长度(bytes)是固定的。

   插入数据:查找适合自己长度的块,然后插入,会有内存浪费。 LRU,闲置>过期 >最少访问

   惰性删除:它并没有提供监控数据过期的机制,而是惰性的,当查询到某个key数据 时,如果过期那么直接抛弃。

   集群搭建原理: Memcache服务器端并没有提供集群功能,但是通过客户端的驱动程序实现了集群配置。 客户端实现集群的原理:首先客户端配置多台集群机器的ip和端口的列表。然后客户端驱动程序在写入之前,首先对key做哈希处理得到哈希值后对总的机器的个数进行取余然后就选择余数对应的机器。

4,封装 MemCache 的工具类:

  

using Memcached.ClientLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CZBK.ItcastOA.Common
{
    public class MemcacheHelper
    {
        private static readonly MemcachedClient mc = null;
        static MemcacheHelper()
        {
            string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };//一定要将地址写到Web.config文件中。
            //初始化池
            SockIOPool pool = SockIOPool.GetInstance();
            pool.SetServers(serverlist);

            pool.InitConnections = 3;
            pool.MinConnections = 3;
            pool.MaxConnections = 5;

            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout = 3000;

            pool.MaintenanceSleep = 30;
            pool.Failover = true;

            pool.Nagle = false;
            pool.Initialize();

            // 获得客户端实例
            mc = new MemcachedClient();
            mc.EnableCompression = false;
        }
        /// <summary>
        /// 向Memcache中写数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Set(string key, object value)
        {
            mc.Set(key, value);
        }
        /// <summary>
        ///  向Memcache中写数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="time">过期时间</param>
        public static void Set(string key, object value, DateTime time)
        {
            mc.Set(key, value, time);
        }
        /// <summary>
        /// 获取Memcahce中的数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object Get(string key)
        {
            return mc.Get(key);
        }
        /// <summary>
        /// 删除Memcache中的数据
        /// </summary>
        /// <param name="key"></param>
        public static bool Delete(string key)
        {
            if (mc.KeyExists(key))
            {
                return mc.Delete(key);
            }
            return false;
        }


    }
}
MemCacheHelper 工具类

 

 

 

原文地址:https://www.cnblogs.com/dlf-myDream/p/5945985.html