init lserp cs 5.0
This commit is contained in:
@@ -0,0 +1,272 @@
|
||||
/******************************************************************************
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// 实行缓存器接口的存储器类
|
||||
/// </summary>
|
||||
public class Cache : ICache
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="setting">缓存器的配置节</param>
|
||||
public Cache(IConfigSetting setting) {
|
||||
this.Init(setting);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="cacheStorage">指定存储器</param>
|
||||
/// <param name="interval">检测的间隔时间</param>
|
||||
public Cache(ICacheStorage cacheStorage, int interval) {
|
||||
this.Init(cacheStorage, interval);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 轮询移除过期的缓存项
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
/// <param name="cacheStorage">指定存储器</param>
|
||||
/// <param name="interval">检测的间隔时间</param>
|
||||
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
|
||||
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
/// <param name="setting">配置节</param>
|
||||
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<ICacheStorage>(true);
|
||||
this.cacheStorage.Init(setting["cacheStorage"]);
|
||||
this.interval = setting.Property["interval"].ToInt32();
|
||||
this.Monitor();
|
||||
this.isInit = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存器名称
|
||||
/// </summary>
|
||||
public string Name {
|
||||
get { return this.name; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存存储器
|
||||
/// </summary>
|
||||
public ICacheStorage CacheStorage {
|
||||
get { return this.cacheStorage; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 以健值方式获取/设置缓存项(值)
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <remarks>
|
||||
/// 如果是设置值,则使用永不过期策略缓存
|
||||
/// </remarks>
|
||||
public object this[string key] {
|
||||
get { return this.Get(key); }
|
||||
set { this.Add(key, value, new NullCacheDependency()); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按指定健值和过期策略来设置缓存项(值)
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <param name="cacheDependency">缓存项的过期策略</param>
|
||||
public object this[string key, ICacheDependency cacheDependency] {
|
||||
set { this.Add(key, value, cacheDependency); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取此缓存器所缓存项的个数
|
||||
/// </summary>
|
||||
public int Count {
|
||||
get {
|
||||
this.IsInit();
|
||||
return this.cacheStorage.Count;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加一项到缓存器中
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <param name="value">缓存的对象</param>
|
||||
/// <param name="cacheDependency">缓存项的过期策略</param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加一项到缓存器中
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <param name="value">缓存的对象</param>
|
||||
/// <remarks>
|
||||
/// 没有指定过期策略,则使用永不过期策略缓存
|
||||
/// </remarks>
|
||||
public void Add(string key, object value) {
|
||||
this.Add(key, value, new NullCacheDependency());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存项
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <returns>缓存的对象,如果缓存中没有命中,则返回<c>null</c></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存的项目
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public CacheItem GetCacheItem(string key)
|
||||
{
|
||||
this.IsInit();
|
||||
return this.cacheStorage[key] as CacheItem;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除缓存项
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
public void Remove(string key) {
|
||||
this.IsInit();
|
||||
this.cacheStorage.Remove(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断缓存器中是否包含指定健值的缓存项
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <returns>是/否</returns>
|
||||
public bool Contains(string key) {
|
||||
this.IsInit();
|
||||
return this.cacheStorage.Contains(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除此缓存器中所有的项
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 不影响配置文件中其他缓存器
|
||||
/// </remarks>
|
||||
public void Clear() {
|
||||
this.IsInit();
|
||||
this.cacheStorage.Clear();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/******************************************************************************
|
||||
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 Lskj.DevFx.ExceptionManagement;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存模块异常
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 在缓存模块里面,能发现的异常都会包装成此类的实例
|
||||
/// </remarks>
|
||||
[Serializable]
|
||||
public class CacheException : BaseException
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public CacheException() : base() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="message">异常消息</param>
|
||||
/// <param name="innerException">内部异常</param>
|
||||
public CacheException(string message, Exception innerException) : base(message, innerException) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="message">异常消息</param>
|
||||
public CacheException(string message) : base(message) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="errorNo">异常编号</param>
|
||||
/// <param name="message">异常消息</param>
|
||||
public CacheException(int errorNo, string message) : base(errorNo, message) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="errorNo">异常编号</param>
|
||||
/// <param name="message">异常消息</param>
|
||||
/// <param name="innerException">内部异常</param>
|
||||
public CacheException(int errorNo, string message, Exception innerException) : base(errorNo, message, innerException) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/******************************************************************************
|
||||
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 Lskj.DevFx.Cache.Config;
|
||||
using Lskj.DevFx.Config;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存实用方法类
|
||||
/// </summary>
|
||||
public static class CacheHelper
|
||||
{
|
||||
#region constructor
|
||||
|
||||
static CacheHelper() {
|
||||
//创建缓存管理器
|
||||
CreateCacheManager();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region private static members
|
||||
|
||||
private static ICacheManager cacheManager;
|
||||
|
||||
private static void CreateCacheManager() {
|
||||
cacheManager = new CacheManager();
|
||||
cacheManager.Init(null);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region public static members
|
||||
|
||||
/// <summary>
|
||||
/// 获取或创建缓存
|
||||
/// </summary>
|
||||
/// <param name="name">缓存名称</param>
|
||||
/// <param name="interval">清除过期缓存项的时间周期,单位:毫秒</param>
|
||||
/// <returns></returns>
|
||||
public static ICache GetOrCreateCache(string name)
|
||||
{
|
||||
return GetOrCreateCache(name, 0);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取或创建缓存
|
||||
/// </summary>
|
||||
/// <param name="name">缓存名称</param>
|
||||
/// <param name="interval">清除过期缓存项的时间周期,单位:毫秒</param>
|
||||
/// <returns></returns>
|
||||
public static ICache GetOrCreateCache(string name,int interval)
|
||||
{
|
||||
ICache cacher = CacheHelper.GetCache(name);
|
||||
if (cacher == null)
|
||||
return CacheHelper.CreateCache(name, interval);
|
||||
else
|
||||
return cacher;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取已配置的缓存器
|
||||
/// </summary>
|
||||
/// <param name="cacheName">配置文件中配置的缓存器名称</param>
|
||||
/// <returns>ICache的实例</returns>
|
||||
public static ICache GetCache(string cacheName) {
|
||||
if(cacheManager != null) {
|
||||
return cacheManager.GetCache(cacheName);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个指定名称的缓存空间
|
||||
/// </summary>
|
||||
/// <param name="cacheName">缓存名称</param>
|
||||
/// <returns>ICache的实例</returns>
|
||||
public static ICache CreateCache(string cacheName)
|
||||
{
|
||||
return CreateCache(cacheName,0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个指定名称的缓存空间
|
||||
/// </summary>
|
||||
/// <param name="cacheName">缓存名称</param>
|
||||
/// <param name="interval">清除过期缓存项的时间周期,单位:毫秒</param>
|
||||
/// <returns>ICache的实例</returns>
|
||||
public static ICache CreateCache(string cacheName,int interval)
|
||||
{
|
||||
if (cacheManager != null)
|
||||
{
|
||||
return cacheManager.AddCache(cacheName,interval);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存项值
|
||||
/// </summary>
|
||||
/// <param name="cacheName">配置文件中配置的缓存器名称</param>
|
||||
/// <param name="key">缓存项健值</param>
|
||||
/// <returns>缓存项值,如果没有命中,则返回<c>null</c></returns>
|
||||
public static object GetCacheValue(string cacheName, string key) {
|
||||
return GetCacheValue(cacheName, key, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存项值
|
||||
/// </summary>
|
||||
/// <param name="cacheName">配置文件中配置的缓存器名称</param>
|
||||
/// <param name="key">缓存项健值</param>
|
||||
/// <param name="throwOnError">如果有错误,是否抛出异常</param>
|
||||
/// <returns>缓存项值,如果没有命中,则返回<c>null</c></returns>
|
||||
public static object GetCacheValue(string cacheName, string key, bool throwOnError) {
|
||||
ICache cache = GetCache(cacheName);
|
||||
object @value = null;
|
||||
if(cache == null) {
|
||||
if(throwOnError) {
|
||||
throw new CacheException("没有配置Cache:" + cacheName);
|
||||
}
|
||||
} else {
|
||||
@value = cache[key];
|
||||
}
|
||||
|
||||
return @value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除所有缓存空间
|
||||
/// </summary>
|
||||
public static void Clear()
|
||||
{
|
||||
if (cacheManager != null)
|
||||
cacheManager.Clear();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/******************************************************************************
|
||||
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;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存项的包装类
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class CacheItem
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项健值</param>
|
||||
/// <param name="value">储存项值</param>
|
||||
/// <param name="cacheDependency">过期策略</param>
|
||||
public CacheItem(string key, object @value, ICacheDependency cacheDependency) {
|
||||
this.key = key;
|
||||
this.value = @value;
|
||||
this.cacheDependency = cacheDependency;
|
||||
this.hits = 0;
|
||||
this.lastAccessTime = DateTime.Now;
|
||||
}
|
||||
|
||||
private string key;
|
||||
private object @value;
|
||||
private ICacheDependency cacheDependency;
|
||||
private int hits;
|
||||
private DateTime lastAccessTime;
|
||||
|
||||
/// <summary>
|
||||
/// 获取过期策略
|
||||
/// </summary>
|
||||
public ICacheDependency CacheDependency {
|
||||
get { return this.cacheDependency; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取健值
|
||||
/// </summary>
|
||||
public string Key {
|
||||
get { return this.key; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存项
|
||||
/// </summary>
|
||||
public object Value {
|
||||
get { return this.value; }
|
||||
set { this.value = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 命中次数
|
||||
/// </summary>
|
||||
public int Hits {
|
||||
get { return this.hits; }
|
||||
set { this.hits = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 最后命中时间
|
||||
/// </summary>
|
||||
public DateTime LastAccessTime {
|
||||
get { return this.lastAccessTime; }
|
||||
set { this.lastAccessTime = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
/******************************************************************************
|
||||
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 Lskj.DevFx.Config;
|
||||
using Lskj.DevFx.Utils;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存管理器实现类,建议应用系统实现此接口的类都从本类继承
|
||||
/// </summary>
|
||||
public class CacheManager : ICacheManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public CacheManager() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="configSetting">配置节</param>
|
||||
public CacheManager(IConfigSetting configSetting) {
|
||||
this.Init(configSetting);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置节
|
||||
/// </summary>
|
||||
protected IConfigSetting setting;
|
||||
private CollectionBase<ICache> caches;
|
||||
private bool isInit;
|
||||
|
||||
private ICache CreateCache(IConfigSetting cacheSetting) {
|
||||
ICache cache = cacheSetting.Property["type"].ToObject<ICache>();
|
||||
cache.Init(cacheSetting);
|
||||
return cache;
|
||||
}
|
||||
|
||||
#region ICacheManager Members
|
||||
|
||||
/// <summary>
|
||||
/// 初始化,由框架调用
|
||||
/// </summary>
|
||||
/// <param name="setting">缓存管理器的配置节</param>
|
||||
public void Init(IConfigSetting setting) {
|
||||
if(this.isInit) {
|
||||
return;
|
||||
}
|
||||
this.setting = setting;
|
||||
this.caches = new CollectionBase<ICache>();
|
||||
if (setting != null)
|
||||
{
|
||||
IConfigSetting[] settings = setting["caches"].GetChildSettings();
|
||||
for (int i = 0; i < settings.Length; i++)
|
||||
{
|
||||
string cacheName = settings[i].Property["name"].Value;
|
||||
if (cacheName == null)
|
||||
{
|
||||
throw new CacheException("缓存存储器名为Null");
|
||||
}
|
||||
if (this.caches.Contains(cacheName))
|
||||
{
|
||||
throw new CacheException("缓存存储器名重复:" + cacheName);
|
||||
}
|
||||
ICache cache = this.CreateCache(settings[i]);
|
||||
this.caches.Add(cacheName, cache);
|
||||
}
|
||||
}
|
||||
this.isInit = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存空间
|
||||
/// </summary>
|
||||
/// <param name="cacheName">在配置文件上配置的缓存空间名</param>
|
||||
/// <returns>实现ICache接口的缓存器实例</returns>
|
||||
public ICache GetCache(string cacheName) {
|
||||
return this.caches[cacheName];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 以一定的配置节来实例化缓存器
|
||||
/// </summary>
|
||||
/// <param name="cacheSetting">缓存器配置节</param>
|
||||
/// <returns>实现ICache接口的缓存器实例</returns>
|
||||
public ICache GetCache(IConfigSetting cacheSetting) {
|
||||
string cacheName = cacheSetting.Property["name"].Value;
|
||||
ICache cache = this.GetCache(cacheName);
|
||||
if(cache == null) {
|
||||
cache = this.CreateCache(cacheSetting);
|
||||
this.caches.Add(cacheName, cache);
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增一个缓存空间
|
||||
/// </summary>
|
||||
/// <param name="cacheName"></param>
|
||||
/// <returns></returns>
|
||||
public ICache AddCache(string cacheName)
|
||||
{
|
||||
return AddCache(cacheName, 0);
|
||||
}
|
||||
/// <summary>
|
||||
/// 新增一个缓存空间
|
||||
/// </summary>
|
||||
/// <param name="cacheName"></param>
|
||||
/// <param name="interval">清除过期缓存的间隔,单位:毫秒</param>
|
||||
/// <returns></returns>
|
||||
public ICache AddCache(string cacheName,int interval)
|
||||
{
|
||||
//判断集合是否为空,如果为空则自动创建
|
||||
if (this.caches == null)
|
||||
{
|
||||
this.caches = new CollectionBase<ICache>();
|
||||
}
|
||||
|
||||
//创建一个内存缓存存储器
|
||||
ICacheStorage cacheStorage = new NullCacheStorage();
|
||||
cacheStorage.Init(null);
|
||||
|
||||
//创建一个永不过期的缓存器,并添加到缓存管理器集合
|
||||
ICache cache = new Cache(cacheStorage, interval);
|
||||
this.caches.Add(cacheName, cache);
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除指定名称的缓存空间
|
||||
/// </summary>
|
||||
/// <param name="cacheName"></param>
|
||||
public void RemoveCache(string cacheName)
|
||||
{
|
||||
if (this.caches == null)
|
||||
return;
|
||||
//如果指定缓存空间存在则进行移除
|
||||
if(this.caches[cacheName]!=null)
|
||||
this.caches.Remove(cacheName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除所有缓存空间
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
//判断集合是否为空
|
||||
if (this.caches != null)
|
||||
{
|
||||
this.caches.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/******************************************************************************
|
||||
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 Lskj.DevFx.Config.DotNetConfig;
|
||||
|
||||
namespace Lskj.DevFx.Cache.Config
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存的配置节信息
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 配置文件格式和说明:
|
||||
/// <code>
|
||||
/// <configSections>
|
||||
/// <sectionGroup name="Lskj.devfx" type="Lskj.DevFx.Config.GroupHandler, Lskj.DevFx.BaseFx">
|
||||
/// <section name="cache" type="Lskj.DevFx.Cache.Config.SectionHandler, Lskj.DevFx.BaseFx" />
|
||||
/// ......
|
||||
/// </sectionGroup>
|
||||
/// </configSections>
|
||||
///
|
||||
/// ......
|
||||
///
|
||||
/// <Lskj.devfx>
|
||||
/// <cache>
|
||||
/// ......
|
||||
/// </cache>
|
||||
/// </Lskj.devfx>
|
||||
/// ......
|
||||
/// </code>
|
||||
/// </remarks>
|
||||
public class SectionHandler : SectionBaseHandler<SectionHandler>
|
||||
{
|
||||
/// <summary>
|
||||
/// 设置为允许未知的元素存在
|
||||
/// </summary>
|
||||
protected override bool OnDeserializeUnrecognizedFlag {
|
||||
get { return true; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/******************************************************************************
|
||||
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;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 时间过期的过期策略(包括相对时间过期、绝对时间过期)
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class ExpirationCacheDependency : ICacheDependency
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数(绝对时间过期方式)
|
||||
/// </summary>
|
||||
/// <param name="absoluteExperation">绝对过期时间</param>
|
||||
public ExpirationCacheDependency(DateTime absoluteExperation) {
|
||||
this.absoluteExperation = absoluteExperation;
|
||||
this.slidingExperation = TimeSpan.MaxValue;
|
||||
this.isSliding = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数(相对时间过期方式)
|
||||
/// </summary>
|
||||
/// <param name="slidingExperation">相对过期时间</param>
|
||||
public ExpirationCacheDependency(TimeSpan slidingExperation) {
|
||||
this.slidingExperation = slidingExperation;
|
||||
this.absoluteExperation = DateTime.Now.Add(slidingExperation);
|
||||
this.isSliding = true;
|
||||
}
|
||||
|
||||
private bool isSliding;
|
||||
private DateTime absoluteExperation;
|
||||
private TimeSpan slidingExperation;
|
||||
|
||||
#region ICacheDependency Members
|
||||
|
||||
/// <summary>
|
||||
/// 是否已过期
|
||||
/// </summary>
|
||||
public bool IsExpired {
|
||||
get {
|
||||
return (this.absoluteExperation < DateTime.Now);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置缓存策略(相当于重新开始缓存),针对于相对时间过期策略有效
|
||||
/// </summary>
|
||||
public void Reset() {
|
||||
if(this.isSliding) {
|
||||
this.absoluteExperation = DateTime.Now.Add(this.slidingExperation);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/******************************************************************************
|
||||
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.IO;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件依赖方式的过期策略
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class FileCacheDependency : ICacheDependency
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="fileName">需要监视的文件名(包含路径)</param>
|
||||
/// <param name="filters">监视方式</param>
|
||||
public FileCacheDependency(string fileName, NotifyFilters filters) {
|
||||
this.fileName = fileName;
|
||||
this.filters = filters;
|
||||
this.init();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="fileName">需要监视的文件名(包含路径)</param>
|
||||
/// <remarks>
|
||||
/// 监视方式默认为文件的最后写入(修改)时间
|
||||
/// </remarks>
|
||||
public FileCacheDependency(string fileName) : this(fileName, NotifyFilters.LastWrite) {
|
||||
}
|
||||
|
||||
private string fileName;
|
||||
private FileSystemWatcher fileWatcher;
|
||||
private NotifyFilters filters;
|
||||
private bool isExpired = false;
|
||||
|
||||
private void init() {
|
||||
string fileName = Path.GetFileName(this.fileName);
|
||||
string filePath = Path.GetDirectoryName(this.fileName);
|
||||
this.fileWatcher = new FileSystemWatcher(filePath, fileName);
|
||||
this.fileWatcher.NotifyFilter = this.filters;
|
||||
this.fileWatcher.Changed += new FileSystemEventHandler(this.FileChanged);
|
||||
this.fileWatcher.EnableRaisingEvents = true;
|
||||
}
|
||||
|
||||
private void FileChanged(object sender, FileSystemEventArgs e) {
|
||||
this.fileWatcher.EnableRaisingEvents = false;
|
||||
this.isExpired = true;
|
||||
this.fileWatcher.Dispose();
|
||||
this.fileWatcher = null;
|
||||
}
|
||||
|
||||
#region ICacheDependency Members
|
||||
|
||||
/// <summary>
|
||||
/// 是否已过期
|
||||
/// </summary>
|
||||
public bool IsExpired {
|
||||
get { return this.isExpired; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置缓存策略(相当于重新开始缓存)
|
||||
/// </summary>
|
||||
public void Reset() {
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/******************************************************************************
|
||||
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 Lskj.DevFx.Config;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存器接口
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 缓存器的配置:
|
||||
/// <code>
|
||||
/// <configuration>
|
||||
/// ......
|
||||
///
|
||||
/// <cache type="Lskj.DevFx.Cache.CacheManager">
|
||||
/// <caches>
|
||||
/// ......
|
||||
/// <cache name="缓存器名称" type="缓存器类型" interval="检查过期的时间间隔,0或小于0表示不进行检查">
|
||||
/// <!--这里配置缓存存储器-->
|
||||
/// <cacheStorage type="Lskj.DevFx.Cache.NullCacheStorage" />
|
||||
/// </cache>
|
||||
/// ......
|
||||
/// </caches>
|
||||
/// </cache>
|
||||
///
|
||||
/// ......
|
||||
/// </configuration>
|
||||
/// </code>
|
||||
/// </remarks>
|
||||
public interface ICache
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
/// <param name="setting">配置节</param>
|
||||
void Init(IConfigSetting setting);
|
||||
|
||||
/// <summary>
|
||||
/// 缓存器名称
|
||||
/// </summary>
|
||||
string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 缓存存储器
|
||||
/// </summary>
|
||||
ICacheStorage CacheStorage { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 以健值方式获取/设置缓存项(值)
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <remarks>
|
||||
/// 如果是设置值,则使用永不过期策略缓存
|
||||
/// </remarks>
|
||||
object this[string key] { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 按指定健值和过期策略来设置缓存项(值)
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <param name="cacheDependency">缓存项的过期策略</param>
|
||||
object this[string key, ICacheDependency cacheDependency] { set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取此缓存器所缓存项的个数
|
||||
/// </summary>
|
||||
int Count { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 添加一项到缓存器中
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <param name="value">缓存的对象</param>
|
||||
/// <param name="cacheDependency">缓存项的过期策略</param>
|
||||
void Add(string key, object @value, ICacheDependency cacheDependency);
|
||||
|
||||
/// <summary>
|
||||
/// 添加一项到缓存器中
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <param name="value">缓存的对象</param>
|
||||
/// <remarks>
|
||||
/// 没有指定过期策略,则使用永不过期策略缓存
|
||||
/// </remarks>
|
||||
void Add(string key, object @value);
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存项
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <returns>缓存的对象,如果缓存中没有命中,则返回<c>null</c></returns>
|
||||
object Get(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存的项目
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
CacheItem GetCacheItem(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 移除缓存项
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
void Remove(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 判断缓存器中是否包含指定健值的缓存项
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项的健值</param>
|
||||
/// <returns>是/否</returns>
|
||||
bool Contains(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 清除此缓存器中所有的项
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 不影响配置文件中其他缓存器
|
||||
/// </remarks>
|
||||
void Clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/******************************************************************************
|
||||
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
|
||||
/*******************************************************************************/
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存过期策略接口
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// 下面的示例演示了以相对时间过期策略来缓存对象:
|
||||
/// <code>
|
||||
/// ......
|
||||
/// string key = Guid.NewGuid().ToString();
|
||||
/// object cachingObject = new YourCachingObject();
|
||||
/// ICache cache = CacheHelper.GetCache("your cache instance name");
|
||||
/// cache.Add(key, cachingObject, new ExpirationCacheDependency(TimeSpan.FromSeconds(20)));
|
||||
/// ......
|
||||
/// </code>
|
||||
/// </example>
|
||||
public interface ICacheDependency
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否已过期
|
||||
/// </summary>
|
||||
bool IsExpired { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 重置缓存策略(相当于重新开始缓存)
|
||||
/// </summary>
|
||||
void Reset();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/******************************************************************************
|
||||
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 Lskj.DevFx.Config;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存管理器接口
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 缓存管理器的配置:
|
||||
/// <code>
|
||||
/// <configuration>
|
||||
/// ......
|
||||
///
|
||||
/// <cache type="缓存管理器类型">
|
||||
/// <caches><!--这里配置缓存空间(多实例模式)-->
|
||||
/// ......
|
||||
/// </caches>
|
||||
/// </cache>
|
||||
///
|
||||
/// ......
|
||||
/// </configuration>
|
||||
/// </code>
|
||||
/// </remarks>
|
||||
public interface ICacheManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化,由框架调用
|
||||
/// </summary>
|
||||
/// <param name="setting">缓存管理器的配置节</param>
|
||||
void Init(IConfigSetting setting);
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存空间
|
||||
/// </summary>
|
||||
/// <param name="cacheName">在配置文件上配置的缓存空间名</param>
|
||||
/// <returns>实现ICache接口的缓存器实例</returns>
|
||||
ICache GetCache(string cacheName);
|
||||
|
||||
/// <summary>
|
||||
/// 以一定的配置节来实例化缓存器
|
||||
/// </summary>
|
||||
/// <param name="cacheSetting">缓存器配置节</param>
|
||||
/// <returns>实现ICache接口的缓存器实例</returns>
|
||||
ICache GetCache(IConfigSetting cacheSetting);
|
||||
|
||||
/// <summary>
|
||||
/// 新增一个缓存空间
|
||||
/// </summary>
|
||||
/// <param name="cacheName"></param>
|
||||
/// <returns></returns>
|
||||
ICache AddCache(string cacheName);
|
||||
|
||||
/// <summary>
|
||||
/// 新增一个缓存空间
|
||||
/// </summary>
|
||||
/// <param name="cacheName"></param>
|
||||
/// <param name="interval">清除过期缓存的间隔,单位:毫秒</param>
|
||||
/// <returns></returns>
|
||||
ICache AddCache(string cacheName, int interval);
|
||||
|
||||
/// <summary>
|
||||
/// 移除指定名称的缓存空间
|
||||
/// </summary>
|
||||
/// <param name="cacheName"></param>
|
||||
void RemoveCache(string cacheName);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 移除所有缓存空间
|
||||
/// </summary>
|
||||
void Clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
/******************************************************************************
|
||||
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 Lskj.DevFx.Config;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存器的存储接口
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 缓存器存储接口的配置:
|
||||
/// <code>
|
||||
/// <configuration>
|
||||
/// ......
|
||||
///
|
||||
/// <cache type="Lskj.DevFx.Cache.CacheManager">
|
||||
/// <caches>
|
||||
/// ......
|
||||
/// <cache name="缓存器名称" type="缓存器类型" interval="检查过期的时间间隔,0或小于0表示不进行检查">
|
||||
/// <!--这里配置缓存存储器-->
|
||||
/// <cacheStorage type="实现缓存存储器接口的类型" />
|
||||
/// </cache>
|
||||
/// ......
|
||||
/// </caches>
|
||||
/// </cache>
|
||||
///
|
||||
/// ......
|
||||
/// </configuration>
|
||||
/// </code>
|
||||
/// </remarks>
|
||||
public interface ICacheStorage
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
/// <param name="setting">配置节</param>
|
||||
void Init(IConfigSetting setting);
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置一个存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
object this[string key] { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置一个存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
object this[int index] { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取此存储器所存储项的个数
|
||||
/// </summary>
|
||||
int Count { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 添加一项到存储器中
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 如果存在相同的健值,则更新存储的对象
|
||||
/// </remarks>
|
||||
void Add(string key, object @value);
|
||||
|
||||
/// <summary>
|
||||
/// 获取存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <returns>存储的对象,如果存储中没有命中,则返回<c>null</c></returns>
|
||||
object Get(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 获取存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
/// <returns>存储的对象,如果存储中没有命中,则返回<c>null</c></returns>
|
||||
object Get(int index);
|
||||
|
||||
/// <summary>
|
||||
/// 设置存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 仅针对存在存储项,若不存在,则不进行任何操作
|
||||
/// </remarks>
|
||||
void Set(string key, object @value);
|
||||
|
||||
/// <summary>
|
||||
/// 设置存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 仅针对存在存储项,若不存在,则不进行任何操作
|
||||
/// </remarks>
|
||||
void Set(int index, object @value);
|
||||
|
||||
/// <summary>
|
||||
/// 移除存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
void Remove(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 在指定的位置移除存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
void RemoveAt(int index);
|
||||
|
||||
/// <summary>
|
||||
/// 判断存储器中是否包含指定健值的存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <returns>是/否</returns>
|
||||
bool Contains(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 清除此存储器中所有的项
|
||||
/// </summary>
|
||||
void Clear();
|
||||
|
||||
/// <summary>
|
||||
/// 获得此存储器中所有项的健值
|
||||
/// </summary>
|
||||
/// <returns>健值列表(数组)</returns>
|
||||
string[] GetAllKeys();
|
||||
|
||||
/// <summary>
|
||||
/// 获取此存储器中所有项的值
|
||||
/// </summary>
|
||||
/// <returns>存储项列表(数组)</returns>
|
||||
object[] GetAllValues();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/******************************************************************************
|
||||
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;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 永不过期的缓存策略
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class NullCacheDependency : ICacheDependency
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public NullCacheDependency() {
|
||||
}
|
||||
|
||||
#region ICacheDependency Members
|
||||
|
||||
/// <summary>
|
||||
/// 是否已过期(永远返回<c>false</c>)
|
||||
/// </summary>
|
||||
public bool IsExpired {
|
||||
get {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置缓存策略(相当于重新开始缓存)
|
||||
/// </summary>
|
||||
public void Reset() {
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
/******************************************************************************
|
||||
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.Collections.Specialized;
|
||||
using Lskj.DevFx.Config;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 内存数据存储方式的存储器
|
||||
/// </summary>
|
||||
public class NullCacheStorage : NameObjectCollectionBase, ICacheStorage
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public NullCacheStorage() : base() {
|
||||
this.IsReadOnly = false;
|
||||
}
|
||||
|
||||
#region ICacheStorage Members
|
||||
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
/// <param name="setting">配置节</param>
|
||||
public void Init(IConfigSetting setting) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 以索引方式获取设置一项存储值
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引</param>
|
||||
public object this[int index] {
|
||||
get { return this.Get(index); }
|
||||
set { this.Set(index, value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置一个存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
public object this[string key] {
|
||||
get { return this.Get(key); }
|
||||
set { this.Set(key, value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加一项到存储器中
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
public void Add(string key, object value) {
|
||||
this.BaseAdd(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <returns>存储的对象,如果存储中没有命中,则返回<c>null</c></returns>
|
||||
public object Get(string key) {
|
||||
return this.BaseGet(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
/// <returns>存储的对象,如果存储中没有命中,则返回<c>null</c></returns>
|
||||
public object Get(int index) {
|
||||
return this.BaseGet(index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 仅针对存在存储项,若不存在,则不进行任何操作
|
||||
/// </remarks>
|
||||
public void Set(string key, object @value) {
|
||||
this.BaseSet(key, @value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 仅针对存在存储项,若不存在,则不进行任何操作
|
||||
/// </remarks>
|
||||
public void Set(int index, object @value) {
|
||||
this.BaseSet(index, @value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
public void Remove(string key) {
|
||||
this.BaseRemove(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在指定的位置移除存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
public void RemoveAt(int index) {
|
||||
this.BaseRemoveAt(index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断存储器中是否包含指定健值的存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <returns>是/否</returns>
|
||||
public bool Contains(string key) {
|
||||
return this.BaseGet(key) != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除此存储器中所有的项
|
||||
/// </summary>
|
||||
public void Clear() {
|
||||
this.BaseClear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获得此存储器中所有项的健值
|
||||
/// </summary>
|
||||
/// <returns>健值列表(数组)</returns>
|
||||
public string[] GetAllKeys() {
|
||||
return this.BaseGetAllKeys();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取此存储器中所有项的值
|
||||
/// </summary>
|
||||
/// <returns>存储项列表(数组)</returns>
|
||||
public object[] GetAllValues() {
|
||||
return this.BaseGetAllValues();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
/******************************************************************************
|
||||
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 Lskj.DevFx.Config;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 远程缓存存储代理类
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 远程缓存存储代理的配置:
|
||||
/// <code>
|
||||
/// <configuration>
|
||||
/// ......
|
||||
///
|
||||
/// <cache type="Lskj.DevFx.Cache.CacheManager">
|
||||
/// <caches>
|
||||
/// ......
|
||||
/// <cache name="缓存器名称" type="缓存器类型" interval="检查过期的时间间隔,0或小于0表示不进行检查">
|
||||
/// <!--这里配置缓存存储器-->
|
||||
/// <cacheStorage type="Lskj.DevFx.Cache.RemoteCacheStorageProxy" url="远端对象的配置,例如:tcp://localhost:8085/RemoteCacheStorage" />
|
||||
/// </cache>
|
||||
/// ......
|
||||
/// </caches>
|
||||
/// </cache>
|
||||
///
|
||||
/// ......
|
||||
/// </configuration>
|
||||
/// </code>
|
||||
/// </remarks>
|
||||
public class RemoteCacheStorageProxy : ICacheStorage
|
||||
{
|
||||
#region private members
|
||||
|
||||
private ICacheStorage remoteStorage;
|
||||
|
||||
private bool isInit = false;
|
||||
private string remoteUrl = null;
|
||||
|
||||
private void GetRemoteObject() {
|
||||
if(this.remoteStorage == null) {
|
||||
this.remoteStorage = (ICacheStorage)Activator.GetObject(typeof(ICacheStorage), this.remoteUrl);
|
||||
}
|
||||
}
|
||||
|
||||
private bool RemotingIsReady() {
|
||||
if(!this.isInit) {
|
||||
return false;
|
||||
}
|
||||
this.GetRemoteObject();
|
||||
if(this.remoteStorage == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
this.remoteStorage.ToString();
|
||||
return true;
|
||||
} catch {
|
||||
//Exceptor.Publish(new CacheException("远程服务器不可用,请检查!", e), LogLevel.EMERGENCY);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICacheStorage Members
|
||||
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
/// <param name="setting">配置节</param>
|
||||
public void Init(IConfigSetting setting) {
|
||||
if(!this.isInit) {
|
||||
this.remoteUrl = setting.Property["url"].Value;
|
||||
this.GetRemoteObject();
|
||||
this.isInit = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置一个存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
public object this[string key] {
|
||||
get {
|
||||
if(this.RemotingIsReady()) {
|
||||
return this.remoteStorage[key];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
set {
|
||||
if(this.RemotingIsReady()) {
|
||||
this.remoteStorage[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置一个存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
public object this[int index] {
|
||||
get {
|
||||
if(this.RemotingIsReady()) {
|
||||
return this.remoteStorage[index];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
set {
|
||||
if(this.RemotingIsReady()) {
|
||||
this.remoteStorage[index] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取此存储器所存储项的个数
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 如果远端对象没有准备好,则返回-1
|
||||
/// </remarks>
|
||||
public int Count {
|
||||
get {
|
||||
if(this.RemotingIsReady()) {
|
||||
return this.remoteStorage.Count;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加一项到存储器中
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 如果存在相同的健值,则更新存储的对象
|
||||
/// </remarks>
|
||||
public void Add(string key, object @value) {
|
||||
if(this.RemotingIsReady()) {
|
||||
this.remoteStorage.Add(key, @value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <returns>存储的对象,如果存储中没有命中,则返回<c>null</c></returns>
|
||||
public object Get(string key) {
|
||||
if(this.RemotingIsReady()) {
|
||||
return this.remoteStorage.Get(key);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
/// <returns>存储的对象,如果存储中没有命中,则返回<c>null</c></returns>
|
||||
public object Get(int index) {
|
||||
if(this.RemotingIsReady()) {
|
||||
return this.remoteStorage.Get(index);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 仅针对存在存储项,若不存在,则不进行任何操作
|
||||
/// </remarks>
|
||||
public void Set(string key, object @value) {
|
||||
if(this.RemotingIsReady()) {
|
||||
this.remoteStorage.Set(key, @value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 仅针对存在存储项,若不存在,则不进行任何操作
|
||||
/// </remarks>
|
||||
public void Set(int index, object @value) {
|
||||
if(this.RemotingIsReady()) {
|
||||
this.remoteStorage.Set(index, @value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
public void Remove(string key) {
|
||||
if(this.RemotingIsReady()) {
|
||||
this.remoteStorage.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在指定的位置移除存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
public void RemoveAt(int index) {
|
||||
if(this.RemotingIsReady()) {
|
||||
this.remoteStorage.RemoveAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断存储器中是否包含指定健值的存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <returns>是/否</returns>
|
||||
public bool Contains(string key) {
|
||||
if(this.RemotingIsReady()) {
|
||||
return this.remoteStorage.Contains(key);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除此存储器中所有的项
|
||||
/// </summary>
|
||||
public void Clear() {
|
||||
if(this.RemotingIsReady()) {
|
||||
this.remoteStorage.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获得此存储器中所有项的健值
|
||||
/// </summary>
|
||||
/// <returns>健值列表(数组)</returns>
|
||||
public string[] GetAllKeys() {
|
||||
if(this.RemotingIsReady()) {
|
||||
return this.remoteStorage.GetAllKeys();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取此存储器中所有项的值
|
||||
/// </summary>
|
||||
/// <returns>存储项列表(数组)</returns>
|
||||
public object[] GetAllValues() {
|
||||
if(this.RemotingIsReady()) {
|
||||
return this.remoteStorage.GetAllValues();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
/******************************************************************************
|
||||
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.Collections.Generic;
|
||||
using Lskj.DevFx.Config;
|
||||
|
||||
namespace Lskj.DevFx.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 存储器远端(服务器)类
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 配合 <see cref="RemoteCacheStorageProxy"/>,把存储数据存储到远端<br />
|
||||
/// 注意:应该把本类配置为Singleton模式
|
||||
/// </remarks>
|
||||
public class RemoteCacheStorageServer : MarshalByRefObject, ICacheStorage
|
||||
{
|
||||
#region private members
|
||||
|
||||
private static List<RemoteCacheStorageServer> instances = new List<RemoteCacheStorageServer>();
|
||||
|
||||
private ICacheStorage cacheStorage = new NullCacheStorage();
|
||||
|
||||
#endregion
|
||||
|
||||
#region constructors
|
||||
|
||||
private RemoteCacheStorageServer() {
|
||||
instances.Add(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region public static members
|
||||
|
||||
/// <summary>
|
||||
/// 获取本类的实例列表
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 利用此属性,可以把客户端轮询过期时间设置为-1,然后在服务器端进行轮询,以提高效率
|
||||
/// </remarks>
|
||||
public static RemoteCacheStorageServer[] Instances {
|
||||
get {
|
||||
return instances.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICacheStorage Members
|
||||
|
||||
/// <summary>
|
||||
/// 添加一项到存储器中
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 如果存在相同的健值,则更新存储的对象
|
||||
/// </remarks>
|
||||
public void Add(string key, object value) {
|
||||
this.cacheStorage.Add(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <returns>存储的对象,如果存储中没有命中,则返回<c>null</c></returns>
|
||||
public object Get(string key) {
|
||||
return this.cacheStorage.Get(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
/// <returns>存储的对象,如果存储中没有命中,则返回<c>null</c></returns>
|
||||
public object Get(int index) {
|
||||
return this.cacheStorage.Get(index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 仅针对存在存储项,若不存在,则不进行任何操作
|
||||
/// </remarks>
|
||||
public void Set(string key, object @value) {
|
||||
this.cacheStorage.Set(key, @value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
/// <param name="value">存储的对象</param>
|
||||
/// <remarks>
|
||||
/// 仅针对存在存储项,若不存在,则不进行任何操作
|
||||
/// </remarks>
|
||||
public void Set(int index, object @value) {
|
||||
this.cacheStorage.Set(index, @value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
public void Remove(string key) {
|
||||
this.cacheStorage.Remove(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在指定的位置移除存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
public void RemoveAt(int index) {
|
||||
this.cacheStorage.RemoveAt(index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断存储器中是否包含指定健值的存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
/// <returns>是/否</returns>
|
||||
public bool Contains(string key) {
|
||||
return this.cacheStorage.Contains(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除此存储器中所有的项
|
||||
/// </summary>
|
||||
public void Clear() {
|
||||
this.cacheStorage.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获得此存储器中所有项的健值
|
||||
/// </summary>
|
||||
/// <returns>健值列表(数组)</returns>
|
||||
public string[] GetAllKeys() {
|
||||
return this.cacheStorage.GetAllKeys();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取此存储器中所有项的值
|
||||
/// </summary>
|
||||
/// <returns>存储项列表(数组)</returns>
|
||||
public object[] GetAllValues() {
|
||||
return this.cacheStorage.GetAllValues();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取此存储器所存储项的个数
|
||||
/// </summary>
|
||||
public int Count {
|
||||
get { return this.cacheStorage.Count; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
/// <param name="setting">配置节</param>
|
||||
public void Init(IConfigSetting setting) {
|
||||
this.cacheStorage.Init(setting);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置一个存储项
|
||||
/// </summary>
|
||||
/// <param name="key">存储项的健值</param>
|
||||
public object this[string key] {
|
||||
get { return this.cacheStorage[key]; }
|
||||
set { this.cacheStorage[key] = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置一个存储项
|
||||
/// </summary>
|
||||
/// <param name="index">存储项的索引值</param>
|
||||
public object this[int index] {
|
||||
get { return this.cacheStorage[index]; }
|
||||
set { this.cacheStorage[index] = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user