设为首页 收藏本站
查看: 1158|回复: 0

[经验分享] memcached缓存机制+微软缓存机制使用详解

[复制链接]

尚未签到

发表于 2015-8-31 13:58:37 | 显示全部楼层 |阅读模式
  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>
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-106859-1-1.html 上篇帖子: 关于memcached的收集 下篇帖子: memcache与memcached扩展的区别
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表