|
大家对这段代码肯定很熟悉吧:
public List SearchUsers(string userName)
{
string cacheKey=string.Format("SearchUsers_{0}", userName);
List users = cache.Find(cacheKey) as List;
if (users == null)
{
users = repository.GetUsersByUserName(userName);
cache.Set(cacheKey, users);
}
return users;
}
class HttpRuntimeCache
{
public object Find(string key)
{
return HttpRuntime.Cache[key];
}
public void Set(string key, object value)
{
HttpRuntime.Cache[key] = value;
}
}
导致了如下这些问题:
- 业务逻辑函数中引入了很多无关的缓存代码,导致DDD模型不够纯
- 更换缓存Provider不方便
- 加入缓存冗余机制不方便
- 没办法同时使用多个缓存系统
- 缓存大对象出现异常,比如Memcache有1M的value限制
有诸多问题,因此我们需要引入缓存子系统来解决上述问题,带来的好处:
- DDD模型更加纯
- 具体的Cache实现机制可以很灵活,比如HttpRuntimeCache, Memcache, Redis可以同时使用
- 加入了Cache冗余机制,不会由于某一台Memcache或者Redis down机导致系统速度很慢,实际上,系统还是会保持飞快(除非backup也down了的情况)
- 开发人员更加致力于核心业务,不会分散注意力
- 缓存位置透明化,都会在xml配置文件中进行配置
解决方案,要用到这2篇文章的技术:C# 代理应用 - Cachable 和 聊聊Memcached的应用。
主要的思路分2个:
模型端:通过代理来嵌入AOP方法,来判断是否需要缓存,有缓存value则直接返回value;缓存value的写入是通过AOP的后置方法写入的,因此不需要在业务函数中写代码,当然也支持代码调用。
Cache核心对象:这个对象要解决一致性hash算法、cache value大对象分解功能、冗余机制
代理嵌入AOP的方法,已经在这篇文章中说明了 C# 代理应用 - Cachable,有兴趣的看看,这里就不说了,我们来主要看看CacheCoordinator对象的实现
结构图如下:
先来看看UML图:
CacheCore代码(算法核心):
public class CacheCore
{
private ICacheCoordinator cacheProvider = null;
public CacheCore(ICacheCoordinator cacheProvider)
{
this.cacheProvider = cacheProvider;
}
public void Set(string location, string key, object value)
{
AssureSerializable(value);
string xml = Serializer2XMLConvert(value);
CacheParsedObject parsedObj = new CacheParsedObject();
string classType = string.Format("{0}", value.GetType().FullName);
if (xml.Length > CacheConfig.CacheConfiguration.MaxCacheEntitySize)
{
/*
key:1@3@ConcreteType
key_1:subvalue1
key_2:subvalue2
key_3:subvalue3
*/
//拆分成更小的单元
int splitCount = xml.Length / CacheConfig.CacheConfiguration.MaxCacheEntitySize;
if (CacheConfig.CacheConfiguration.MaxCacheEntitySize * splitCount < xml.Length)
splitCount++;
parsedObj.MainObject = new KeyValuePair(key, string.Format("1@{0}@{1}", splitCount, classType));
for (int i = 0; i < splitCount;i++ )
{
if (i == splitCount - 1) //最后一段,直接截取到最后,不用给出长度
parsedObj.SplittedElements.Add(xml.Substring(i * CacheConfig.CacheConfiguration.MaxCacheEntitySize));
else //其他,要给出长度
parsedObj.SplittedElements.Add(xml.Substring(i * CacheConfig.CacheConfiguration.MaxCacheEntitySize, CacheConfig.CacheConfiguration.MaxCacheEntitySize));
}
}
else
{
/*
key:1@1@ConcreteType
key_1:value
*/
parsedObj.MainObject = new KeyValuePair(key, string.Format("1@1@{0}", classType));
parsedObj.SplittedElements.Add(xml);
}
//针对CacheParsedObject进行逐项保存
this.cacheProvider.Put(parsedObj.MainObject.Key, parsedObj.MainObject.Value);
int curIndex = 0;
foreach(string xmlValue in parsedObj.SplittedElements)
{
curIndex++;
string tkey=string.Format("{0}_{1}", parsedObj.MainObject.Key, curIndex);
this.cacheProvider.Put(tkey, xmlValue);
}
}
public object Get(string location, string key)
{
string mainObjKeySetting = (string)cacheProvider.Get(key);
if (mainObjKeySetting == null || mainObjKeySetting.Length == 0)
return null;
string classType;
CacheParsedObject parsedObj;
GetParsedObject(key, mainObjKeySetting, out classType, out parsedObj);
string xmlValue=string.Empty;
parsedObj.SplittedElements.ForEach(t=>xmlValue+=t);
using (StringReader rdr = new StringReader(xmlValue))
{
//Assembly.Load("Core");
Type t = Type.GetType(classType);
XmlSerializer serializer = new XmlSerializer(t);
return serializer.Deserialize(rdr);
}
}
public void Remove(string location, string key)
{
string mainObjKeySetting = (string)cacheProvider.Get(key);
if (mainObjKeySetting == null || mainObjKeySetting.Length == 0)
return;
string classType;
CacheParsedObject parsedObj;
GetParsedObject(key, mainObjKeySetting, out classType, out parsedObj);
int i = 1;
parsedObj.SplittedElements.ForEach(t => this.cacheProvider.Remove(string.Format("{0}_{1}", parsedObj.MainObject.Key, i++)));
this.cacheProvider.Remove(parsedObj.MainObject.Key);
}
private void GetParsedObject(string key, string mainObjKeySetting, out string classType, out CacheParsedObject parsedObj)
{
int from = 1, end = 1;
classType = string.Empty;
if (mainObjKeySetting.IndexOf('@') > 0)
{
end = int.Parse(mainObjKeySetting.Split('@')[1]);
classType = mainObjKeySetting.Split('@')[2];
}
parsedObj = new CacheParsedObject();
parsedObj.MainObject = new KeyValuePair(key, string.Format("1@{0}@{1}", end, classType));
for (int i = from; i |
|
|