• memcached缓存机制+微软缓存机制使用详解


    1、 why Memcached

    1.1   一台web服务器上,iis接收的请求数是有限的,当访问量超大的时候,网站访问就会遇到瓶颈了,处理方式就是运用多了服务器把请求数分流(集群),对外公布的就一个公共的ip。

    1.2   当数据访问量有10w时候,通过3台服务器分流请求,每台即承担了3.3w个请求,当用户登录的时候,如何共享登录信息就成为需要解决的问题(如把登录信息放到数据库中,性能会很差),这就是分布式缓存的运用场景。

    2、 Memcached基本介绍

    2.1 key最大:255字节,value最大:1m,{key:key1,value:123}

    3、Windows下使用Memcache

    3.1 下载memcache:www.code.jellycan.com/Memcache

    3.2 安装服务:cmd---Memcached.exe –d install

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

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

    3.5 卸载Memcached.exe –d uninstall

    遇到问题:如无法启动此程序,解决方法:下载MSVCR71.dll,安装上即可。

    3.6 增删改查输命令方式

    3.6.1 add keyname 0 0 5 回车 //第一个0是一个数字,第二个是过期时间,单位秒,0表示不限期,5表示value长度,如keyname存在,则不做操作

    12345 //value内容

    3.6.2 get keyname //得到相应的value的值

    3.6.3 delete keyname //删除

    3.6.4 set keyname 0 0 5 回车 //如果没有则添加,如有就更新

         12345

    3、 c#下操作memcache

    4、 微软缓存方式

    demo

    业务逻辑层:

    接口:ICacheManager

    1 public interface ICacheManager
    2     {
    3         object Get(string key);
    4         void Set(string key, object value);
    5         void Set(string key, object value, int timeout);
    6         void Remove(string key);
    7         void RemoveAll();
    8     }

    CacheFactory类:

     1 public class CacheFactory
     2     {
     3         private static ICacheManager _instance = null;
     4         private static object m_LockObj = new object();
     5         private CacheFactory() { }
     6         static CacheFactory()
     7         {
     8             GetInstance();
     9         }
    10         public static ICacheManager GetInstance()
    11         {
    12             if (_instance == null)
    13             {
    14                 lock (m_LockObj)
    15                 {
    16                     if (_instance == null)
    17                     {
    18                         string cacheType = ConfigurationSettings.AppSettings["CacheType"];
    19                         if (cacheType == "MemCacheManager")
    20                             _instance = new MemCachedManager();
    21                         else
    22                             _instance = new MsCacheManager();
    23                     }
    24                 }
    25             }
    26             return _instance;
    27         }
    28     }

    MemCachedManager类:

     1 public class MemCachedManager: ICacheManager
     2     {
     3         private MemcachedClient m_CacheManager;
     4 
     5         public MemCachedManager()
     6         {
     7             m_CacheManager = MemcachedClient.GetInstance("CachePS");
     8         }
     9         public void Set(string key, object value)
    10         {
    11             m_CacheManager.Set(key, value);
    12         }
    13         public void Set(string key, object value, int timeout)
    14         {
    15             m_CacheManager.Set(key, value, DateTime.Now.AddMinutes(timeout));
    16         }
    17         public object Get(string key)
    18         {
    19             return m_CacheManager.Get(key);
    20         }
    21         public void Remove(string key)
    22         {
    23             m_CacheManager.Delete(key);
    24         }
    25         public void RemoveAll()
    26         {
    27             m_CacheManager.FlushAll();
    28         }
    29     }

    MsCacheManager类:

     1 public class MsCacheManager : ICacheManager
     2     {
     3         private BaseCacheDAL m_CacheManager;
     4 
     5         public MsCacheManager()
     6         {
     7             m_CacheManager = new BaseCacheDAL("CachePS");
     8         }
     9 
    10         public object Get(string key)
    11         {
    12             return m_CacheManager.GetCache(key);
    13         }
    14 
    15         public void Set(string key, object value)
    16         {
    17             m_CacheManager.SetCache(key, value, 0);
    18         }
    19 
    20         public void Set(string key, object value, int timeout)
    21         {
    22             m_CacheManager.SetCache(key, value, timeout);
    23         }
    24 
    25         public void Remove(string key)
    26         {
    27             m_CacheManager.Remove(key);
    28         }
    29 
    30         public void RemoveAll()
    31         {
    32             m_CacheManager.RemoveAll();
    33         }
    34     }

    调用:

     1  public static DataTable GetCarPList(int UserId)
     2         {
     3             DataTable dt;
     4             dt = cacheManger.Get(CacheKey.GetCarPList_Key()) as DataTable;
     5             if (dt == null)
     6             {
     7                 string sql = string.Format("select C.CarId,C.ProId,C.UserId,C.BuyNumber,P.ProName,P.Price,P.ProImage,P.Stock  from Car C inner join Product P on C.ProId=P.ProId where C.UserId={0}", UserId);
     8                 dt = SqlHelper.ExecuteDataTable(com.Model.Base.DataBaseEnum.ruanmou, sql, CommandType.Text, null);
     9                 cacheManger.Set(CacheKey.GetCarPList_Key(), dt, 60);
    10             }
    11             return dt;
    12         }

    web.config重要节点配置:

    1 <appSettings>
    2     <add key="CacheType" value="MsCacheManager"/>
    3     <!--<add key="CacheType" value="MemCacheManager"/>-->
    4   </appSettings>
    1 <configSections>
    2     <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    3     <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    4     <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    5     <section name="memcachedgarden" type="System.Configuration.NameValueSectionHandler"/>
    6   </configSections>
     1 <cachingConfiguration defaultCacheManager="CachePS">
     2     <cacheManagers>
     3       <add expirationPollFrequencyInSeconds="120" maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" backingStoreName="Null Storage" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="CachePS"/>
     4     </cacheManagers>
     5     <backingStores>
     6       <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Null Storage"/>
     7     </backingStores>
     8   </cachingConfiguration>
     9   <enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
    10     <sources>
    11       <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    12     </sources>
    13   </enterpriseLibrary.ConfigurationSource>
    14   <memcachedgarden>
    15     <add key="CachePS" value="127.0.0.1:11211"/>
    16   </memcachedgarden>
  • 相关阅读:
    django with mysql (part-4)
    django with mysql (part-3)
    django with mysql (part-2)
    [LeetCode] 22. 括号生成(回溯/DP)
    [算法]求满足要求的进制(辗转相除(欧几里得算法),求最大公约数gcd)
    [算法]体积不小于V的情况下的最小价值(0-1背包)
    [LeetCode]96. 不同的二叉搜索树(DP,卡特兰数)
    [LeetCode]98. 验证二叉搜索树
    [LeetCode]21. 合并两个有序链表(递归)
    [LeetCode]538. 把二叉搜索树转换为累加树
  • 原文地址:https://www.cnblogs.com/ruanmou001/p/3681071.html
Copyright © 2020-2023  润新知