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 ICacheManager2 {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 else22 _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>
新聞熱點
疑難解答