接口文件:
//IDataCache.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ypsuit.common
{
public interface IDataCache
{
/// <summary>
/// 获取缓存
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">缓存key</param>
/// <returns></returns>
T Get<T>(string key);
T Get<T>(string key,string depFile);
/// <summary>
/// 写入缓存
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">缓存key</param>
/// <param name="value">缓存值</param>
/// <returns>返回值,表示:是否写入成功</returns>
bool Set<T>(string key, T value);
/// <summary>
/// 写入缓存,设置过期时间点
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">缓存key</param>
/// <param name="value">缓存值</param>
/// <param name="expiresAt">过期时间点</param>
/// <returns>返回值,表示:是否写入成功</returns>
bool Set<T>(string key, T value, DateTime expiresAt);
/// <summary>
/// 写入缓存,设置过期秒数
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">缓存key</param>
/// <param name="value">缓存值</param>
/// <param name="expiresSecond">过期秒数</param>
/// <returns>返回值,表示:是否写入成功</returns>
bool Set<T>(string key, T value, int expiresSecond);
bool Set<T>(string key, T value, string depFile);
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="key">缓存key</param>
/// <returns></returns>
int Delete(string key);
/// <summary>
/// 删除多个缓存
/// </summary>
/// <param name="keys">缓存key数组</param>
/// <returns></returns>
int Delete(string[] keys);
}
}//实现文件
//RedisCache.cs
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ypsuit.common
{
/// <summary>
/// Redis缓存服务器
/// 服务器和客户端下载:
/// https://github.com/MSOpenTech/redis/releases
/// https://github.com/ServiceStack/ServiceStack.Redis
/// </summary>
public class RedisCache : IDataCache
{
private static RedisClient _redis = null;
public static RedisClient redis
{
get
{
if (_redis == null) _redis = new RedisClient("192.168.9.128", 6379);//要开启服务器才能连接
return _redis;
}
}
~RedisCache()
{
//if (_redis != null) _redis.Shutdown();
}
/// <summary>
/// 获取缓存
/// </summary>
/// <typeparam name="T">类型(对象必须可序列化,否则可以作为object类型取出再类型转换,不然会报错)</typeparam>
/// <param name="key">缓存key</param>
/// <returns></returns>
public T Get<T>(string key)
{
return redis.Get<T>(key);
}
public T Get<T>(string key, string depFile)
{
string timeKey = key + "_time";
if (redis.Exists(timeKey) > 0 && redis.Exists(key) > 0)
{
DateTime obj_time = Get<DateTime>(timeKey);
T obj_cache = Get<T>(key);
if (File.Exists(depFile))
{
FileInfo fi = new FileInfo(depFile);
if (obj_time != fi.LastWriteTime)
{
Delete(key);
Delete(timeKey);
return default(T);
}
else return obj_cache;
}
else
{
throw new Exception("文件(" + depFile + ")不存在!");
}
}
else return default(T);
}
public bool Set<T>(string key, T value)
{
return redis.Set<T>(key, value);
}
public bool Set<T>(string key, T value, DateTime expiresAt)
{
return redis.Set<T>(key, value, expiresAt);
}
public bool Set<T>(string key, T value, int expiresSecond)
{
return redis.Set<T>(key, value, DateTime.Now.AddSeconds(expiresSecond));
}
public bool Set<T>(string key, T value, string depFile)
{
bool ret1 = redis.Set<T>(key, value);
if (ret1 && File.Exists(depFile))
{
FileInfo fi = new FileInfo(depFile);
DateTime lastWriteTime = fi.LastWriteTime;
return redis.Set<DateTime>(key + "_time", lastWriteTime);
}
return false;
}
public int Delete(string key)
{
return redis.Del(key);
}
public int Delete(string[] keys)
{
return redis.Del(keys);
}
public void Dispose()
{
if (_redis != null) _redis.Shutdown();//调用Dispose释放memcached客户端连接
}
}
}
//实现文件
//MemcachedCache.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Memcached.ClientLibrary;
using System.IO;
namespace ypsuit.common
{
public class MemcachedCache:IDataCache
{
private MemcachedClient _mc =null;
protected MemcachedClient mc
{
get
{