国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

memcached緩存機(jī)制+微軟緩存機(jī)制使用詳解

2019-11-17 01:30:15
字體:
供稿:網(wǎng)友

memcached緩存機(jī)制+微軟緩存機(jī)制使用詳解

1、 why Memcached

1.1 一臺web服務(wù)器上,iis接收的請求數(shù)是有限的,當(dāng)訪問量超大的時(shí)候,網(wǎng)站訪問就會遇到瓶頸了,處理方式就是運(yùn)用多了服務(wù)器把請求數(shù)分流(集群),對外公布的就一個(gè)公共的ip

1.2 當(dāng)數(shù)據(jù)訪問量有10w時(shí)候,通過3臺服務(wù)器分流請求,每臺即承擔(dān)了3.3w個(gè)請求,當(dāng)用戶登錄的時(shí)候,如何共享登錄信息就成為需要解決的問題(如把登錄信息放到數(shù)據(jù)庫中,性能會很差),這就是分布式緩存的運(yùn)用場景。

2、 Memcached基本介紹

2.1 key最大:255字節(jié),value最大:1m,{key:key1,value:123}

3、Windows下使用Memcache

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

3.2 安裝服務(wù):cmd---Memcached.exe –d install

3.3 啟動服務(wù):cmd---Memcached.exe –d start (restart 重啟,stop關(guān)閉)

3.4 檢查服務(wù)是否啟動:連接到Memcache控制臺:telnet 127.0.0.1 11211,回車,輸入命令:stats 檢查當(dāng)前服務(wù)狀態(tài)。

3.5 卸載Memcached.exe –d uninstall

遇到問題:如無法啟動此程序,解決方法:下載MSVCR71.dll,安裝上即可。

3.6 增刪改查輸命令方式

3.6.1 add keyname 0 0 5 回車 //第一個(gè)0是一個(gè)數(shù)字,第二個(gè)是過期時(shí)間,單位秒,0表示不限期,5表示value長度,如keyname存在,則不做操作

12345 //value內(nèi)容

3.6.2 get keyname //得到相應(yīng)的value的值

3.6.3 delete keyname //刪除

3.6.4 set keyname 0 0 5 回車 //如果沒有則添加,如有就更新

12345

3、 c#下操作memcache

4、 微軟緩存方式

demo

業(yè)務(wù)邏輯層:

接口: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     }

調(diào)用:

 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重要節(jié)點(diǎn)配置:

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>


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 建昌县| 浏阳市| 繁峙县| 衡东县| 韶山市| 广德县| 隆林| 玉树县| 林西县| 重庆市| 鹿邑县| 德安县| 金堂县| 山西省| 年辖:市辖区| 广昌县| 武鸣县| 临潭县| 吉木乃县| 柘荣县| 饶河县| 莱西市| 顺平县| 海晏县| 彭泽县| 浦东新区| 日土县| 大港区| 保山市| 夏河县| 徐闻县| 绥宁县| 濉溪县| 溧水县| 浪卡子县| 阿尔山市| 会泽县| 同德县| 中江县| 荥经县| 钦州市|