///
/// 基于CacheManager的接口处理
///
public class CacheManager : ICacheManager
{
///
/// ICacheManager对象
///
public ICacheManager
然后在Autofac的配置文件中配置缓存的相关信息,如下文件所示。
如果直接使用Autofac的构造类来处理,那么调用缓存处理的代码如下所示。
//通过AutoFac工厂获取对应的接口实现
var cache = AutoFactory.Instatnce.Container.Resolve();
if (cache != null)
{
accountInfo = cache.Manager.Get(key) as AccountInfo;
if (accountInfo == null)
{
var value = BLLFactory.Instance.FindByID(accountId);
var item = new CacheItem
如果为了使用方便,我们还可以对这个辅助类进行进一步的封装,以便对它进行统一的调用处理即可。
///
/// 基于.NET CacheManager的缓存管理,文档参考:http://cachemanager.michaco.net/documentation
///
public class CacheManagerHelper
{
///
/// 锁定处理变量
///
private static readonly object locker = new object();
///
/// 创建一个缓存的键值,并指定响应的时间范围,如果失效,则自动获取对应的值
///
/// 对象类型
/// 对象的键
/// 获取缓存值的操作
/// 失效的时间范围
/// 失效类型
///
public static T GetCacheItem(string key, Func cachePopulate, TimeSpan expiration,
string region = "_", ExpirationMode mode = ExpirationMode.Sliding) where T :class
{
CacheItem
不过由于官方已经提供了一个类似上面的代码逻辑的TryGetOrAdd方法,这个方法的定义如下所示。 TryGetOrAdd(String, String, Func, out TCacheValue) Tries to either retrieve an existing item or add the item to the cache if it does not exist. The valueFactory will be evaluated only if the item does not exist.
Func valueFactory The method which creates the value which should be added.
TCacheValue value The cache value.
Returns Type Description
Boolean True if the operation succeeds, False in case there are too many retries or the valueFactory returns null.
我们根据这个参数的定义,可以进一步简化上面的辅助类代码。
cache.Manager.TryGetOrAdd(key, region, (_key, _region) =>{
var value = cachePopulate();
var item = new CacheItem(key, region, value, mode, expiration);
return item;
}, out outItem);
return outItem as T;
整个类的代码如下所示
///
/// 基于.NET CacheManager的缓存管理,文档参考:http://cachemanager.michaco.net/documentation
///
public class CacheManagerHelper
{
///
/// 创建一个缓存的键值,并指定响应的时间范围,如果失效,则自动获取对应的值
///
/// 对象类型
/// 对象的键
/// 获取缓存值的操作
/// 失效的时间范围
/// 失效类型
///
public static T GetCacheItem(string key, Func cachePopulate, TimeSpan expiration,
string region = "_", ExpirationMode mode = ExpirationMode.Sliding) where T :class
{
CacheItem outItem = null;
//通过AutoFac工厂获取对应的接口实现
var cache = AutoFactory.Instatnce.Container.Resolve();
if (cache != null)
{
cache.Manager.TryGetOrAdd(key, region, (_key, _region) =>{
var value = cachePopulate();
var item = new CacheItem(key, region, value, mode, expiration);
return item;
}, out outItem);
return outItem as T;
}
else
{
throw new ArgumentNullException("AutoFac配置参数错误,请检查autofac.config是否存在ICacheManager的定义");
}
}
}
web.xml报错
The content of element type "web-app" must match "(icon?,display-
name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,s
JUnit4:Test文档中的解释:
The Test annotation supports two optional parameters.
The first, expected, declares that a test method should throw an exception.
If it doesn't throw an exception or if it
借鉴网上的思路,用java实现:
public class NoIfWhile {
/**
* @param args
*
* find x=1+2+3+....n
*/
public static void main(String[] args) {
int n=10;
int re=find(n);
System.o
在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory。
分析:这是不同系统编码格式引起的:在windows系统中编辑的.sh文件可能有不可见字符,所以在Linux系统下执行会报以上异常信息。
解决:
1)在windows下转换:
利用一些编辑器如UltraEdit或EditPlus等工具
Binary search tree works well for a wide variety of applications, but they have poor worst-case performance. Now we introduce a type of binary search tree where costs are guaranteed to be loga