/******************************************************************************
Copyright 2005-2007 R2@DevFx.NET
DevFx.NET is free software; you can redistribute it and/or modify
it under the terms of the Lesser GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
DevFx.NET is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Lesser GNU General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/*******************************************************************************/
using System;
using System.Timers;
using Lskj.DevFx.Config;
namespace Lskj.DevFx.Cache
{
///
/// 实行缓存器接口的存储器类
///
public class Cache : ICache
{
///
/// 构造函数
///
/// 缓存器的配置节
public Cache(IConfigSetting setting) {
this.Init(setting);
}
///
/// 构造函数
///
/// 指定存储器
/// 检测的间隔时间
public Cache(ICacheStorage cacheStorage, int interval) {
this.Init(cacheStorage, interval);
}
///
/// 构造函数
///
public Cache() {
}
private bool isInit;
private IConfigSetting setting;
private string name;
private ICacheStorage cacheStorage;
private int interval;
private Timer timer;
private bool IsInit() {
if(!this.isInit) {
throw new CacheException("缓存存储器没有被正确初始化");
}
return this.isInit;
}
private void Monitor() {
if(this.interval <= 0) {
return;
}
this.timer = new Timer(this.interval);
this.timer.Enabled = false;
this.timer.AutoReset = false;
this.timer.Elapsed += new ElapsedEventHandler(TimerOnElapsed);
this.timer.Start();
}
///
/// 轮询移除过期的缓存项
///
///
///
private void TimerOnElapsed(object sender, ElapsedEventArgs e) {
this.timer.Enabled = false;
for(int i = 0; i < this.cacheStorage.Count;) {
CacheItem item = (CacheItem)this.cacheStorage[i];
if(item != null && item.CacheDependency.IsExpired) {
this.cacheStorage.RemoveAt(i);
} else {
i++;
}
}
this.timer.Start();
}
///
/// 初始化
///
/// 指定存储器
/// 检测的间隔时间
public void Init(ICacheStorage cacheStorage, int interval) {
if(this.isInit) {
return;
}
this.cacheStorage = cacheStorage;
this.interval = interval;
this.Monitor();
this.isInit = true;
}
#region ICache Members
///
/// 初始化
///
/// 配置节
public void Init(IConfigSetting setting) {
if(this.isInit) {
return;
}
this.setting = setting;
this.name = this.setting.Property["name"].Value;
this.cacheStorage = setting["cacheStorage"].Property["type"].ToObject(true);
this.cacheStorage.Init(setting["cacheStorage"]);
this.interval = setting.Property["interval"].ToInt32();
this.Monitor();
this.isInit = true;
}
///
/// 缓存器名称
///
public string Name {
get { return this.name; }
}
///
/// 缓存存储器
///
public ICacheStorage CacheStorage {
get { return this.cacheStorage; }
}
///
/// 以健值方式获取/设置缓存项(值)
///
/// 缓存项的健值
///
/// 如果是设置值,则使用永不过期策略缓存
///
public object this[string key] {
get { return this.Get(key); }
set { this.Add(key, value, new NullCacheDependency()); }
}
///
/// 按指定健值和过期策略来设置缓存项(值)
///
/// 缓存项的健值
/// 缓存项的过期策略
public object this[string key, ICacheDependency cacheDependency] {
set { this.Add(key, value, cacheDependency); }
}
///
/// 获取此缓存器所缓存项的个数
///
public int Count {
get {
this.IsInit();
return this.cacheStorage.Count;
}
}
///
/// 添加一项到缓存器中
///
/// 缓存项的健值
/// 缓存的对象
/// 缓存项的过期策略
public void Add(string key, object value, ICacheDependency cacheDependency) {
this.IsInit();
CacheItem item = (CacheItem)this.cacheStorage[key];
if(item == null) {
item = new CacheItem(key, value, cacheDependency);
} else {
item.Value = value;
}
this.cacheStorage[key] = item;
}
///
/// 添加一项到缓存器中
///
/// 缓存项的健值
/// 缓存的对象
///
/// 没有指定过期策略,则使用永不过期策略缓存
///
public void Add(string key, object value) {
this.Add(key, value, new NullCacheDependency());
}
///
/// 获取缓存项
///
/// 缓存项的健值
/// 缓存的对象,如果缓存中没有命中,则返回null
public object Get(string key) {
this.IsInit();
CacheItem item = (CacheItem)this.cacheStorage[key];
object @value = null;
if(item != null) {
if(!item.CacheDependency.IsExpired) {
@value = item.Value;
item.Hits++;
item.LastAccessTime = DateTime.Now;
item.CacheDependency.Reset();
} else {
item = null;
this.cacheStorage.Remove(key);
}
}
return @value;
}
///
/// 获取缓存的项目
///
///
///
public CacheItem GetCacheItem(string key)
{
this.IsInit();
return this.cacheStorage[key] as CacheItem;
}
///
/// 移除缓存项
///
/// 缓存项的健值
public void Remove(string key) {
this.IsInit();
this.cacheStorage.Remove(key);
}
///
/// 判断缓存器中是否包含指定健值的缓存项
///
/// 缓存项的健值
/// 是/否
public bool Contains(string key) {
this.IsInit();
return this.cacheStorage.Contains(key);
}
///
/// 清除此缓存器中所有的项
///
///
/// 不影响配置文件中其他缓存器
///
public void Clear() {
this.IsInit();
this.cacheStorage.Clear();
}
#endregion
}
}