
正文
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
public interface ICacheManager
{
object Get(string key);
void Set(string key, object value);
void Set(string key, object value, int timeout);
void Remove(string key);
void RemoveAll();
}
CacheFactory类:
public class CacheFactory
{
private static ICacheManager _instance = null;
private static object m_LockObj = new object();
private CacheFactory() { }
static CacheFactory()
{
GetInstance();
}
public static ICacheManager GetInstance()
{
if (_instance == null)
{
lock (m_LockObj)
{
if (_instance == null)
{
string cacheType = ConfigurationSettings.AppSettings["CacheType"];
if (cacheType == "MemCacheManager")
_instance = new MemCachedManager();
else
_instance = new MsCacheManager();
}
}
}
return _instance;
}
}
MemCachedManager类:
public class MemCachedManager: ICacheManager
{
private MemcachedClient m_CacheManager; public MemCachedManager()
{
m_CacheManager = MemcachedClient.GetInstance("CachePS");
}
public void Set(string key, object value)
{
m_CacheManager.Set(key, value);
}
public void Set(string key, object value, int timeout)
{
m_CacheManager.Set(key, value, DateTime.Now.AddMinutes(timeout));
}
public object Get(string key)
{
return m_CacheManager.Get(key);
}
public void Remove(string key)
{
m_CacheManager.Delete(key);
}
public void RemoveAll()
{
m_CacheManager.FlushAll();
}
}
MsCacheManager类:
public class MsCacheManager : ICacheManager
{
private BaseCacheDAL m_CacheManager; public MsCacheManager()
{
m_CacheManager = new BaseCacheDAL("CachePS");
} public object Get(string key)
{
return m_CacheManager.GetCache(key);
} public void Set(string key, object value)
{
m_CacheManager.SetCache(key, value, );
} public void Set(string key, object value, int timeout)
{
m_CacheManager.SetCache(key, value, timeout);
} public void Remove(string key)
{
m_CacheManager.Remove(key);
} public void RemoveAll()
{
m_CacheManager.RemoveAll();
}
}
调用:
public static DataTable GetCarPList(int UserId)
{
DataTable dt;
dt = cacheManger.Get(CacheKey.GetCarPList_Key()) as DataTable;
if (dt == null)
{
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);
dt = SqlHelper.ExecuteDataTable(com.Model.Base.DataBaseEnum.ruanmou, sql, CommandType.Text, null);
cacheManger.Set(CacheKey.GetCarPList_Key(), dt, );
}
return dt;
}
web.config重要节点配置:
<appSettings>
<add key="CacheType" value="MsCacheManager"/>
<!--<add key="CacheType" value="MemCacheManager"/>-->
</appSettings>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<section name="memcachedgarden" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<cachingConfiguration defaultCacheManager="CachePS">
<cacheManagers>
<add expirationPollFrequencyInSeconds="" maximumElementsInCacheBeforeScavenging="" numberToRemoveWhenScavenging="" backingStoreName="Null Storage" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="CachePS"/>
</cacheManagers>
<backingStores>
<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"/>
</backingStores>
</cachingConfiguration>
<enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
<sources>
<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"/>
</sources>
</enterpriseLibrary.ConfigurationSource>
<memcachedgarden>
<add key="CachePS" value="127.0.0.1:11211"/>
</memcachedgarden>






