Asp.Net Core 2.0 项目实战(5)Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。
Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了
Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架
Asp.Net Core 2.0 项目实战(3)NCMVC角色权限管理前端UI预览及下载
Asp.Net Core 2.0 项目实战(4)ADO.NET操作数据库封装、 EF Core操作及实例
Asp.Net Core 2.0 项目实战(5)Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。
Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例
Asp.Net Core 2.0 项目实战(7)MD5加密、AES&DES对称加解密
Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类
Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例
Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录
Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级
Asp.Net Core2.0下使用memcached缓存。
Memcached目前微软暂未支持,暂只支持Redis,由于项目历史原因,先用博客园开源项目EnyimMemcachedCore,后续用到的时候在详细完善测试。
MemcachedHelper此类待完善,仅供参考,注释掉66和179行可用。net core下替代HttpContext.Current.Application.Lock()方法暂未找到。
public class MemcachedHelper
{
private static string _DEPEND_ = "mcache_default_depend";
private static string _DICT_CACHE_ = "default_core_depend_dictiaonry";
private static int _EXP_ = * ; //默认缓存10分钟
private static int HH = ; //1小时=3600秒 static readonly object mlock = new object();
private static readonly ILoggerFactory _loggerFacotry = new LoggerFactory();
/// <summary>
/// 定义一个静态MemcachedClient客户端,它随类一起加载,所有对象共用
/// </summary>
private static MemcachedClient mclient;
/// <summary>
/// 构造函数,连接memcachedcore并为KEYS字典开辟储存空间
/// </summary>
static MemcachedHelper()
{
//mclient = MemCached.getInstance();
if (mclient == null)
{
lock (mlock)
{
if (mclient == null)
{
var options = new MemcachedClientOptions();
UtilConf.Configuration.GetSection("enyimMemcached").Bind(options);
mclient = new MemcachedClient(_loggerFacotry, new MemcachedClientConfiguration(_loggerFacotry, options));
}
}
}
//在缓存中开辟一个专门用来存储Kyes的字典对象
MDictionary_SaveDict(new Dictionary<string, List<string>>());
} #region ** 获取缓存 **
/// <summary>
/// 获取缓存
/// </summary>
public static object Get(string key)
{
key = key.ToLower();
return mclient.Get(key);
}
#endregion #region ** 添加缓存 **
/// <summary>
/// 添加单个依赖项的缓存 (最小时间单位为秒)
/// </summary>
public static void Set(string depend, string key, object obj, int exp)
{
depend = depend.ToLower();
key = key.ToLower(); try
{
//HttpContext.Current.Application.Lock(); //将数据加入缓存
mclient.Add(key, obj, exp); //HttpContext.Current.Application.UnLock(); ////将Keys加入字典
//MDictionary_AddKeys(depend, key);
}
catch (System.Exception ex)
{
throw new Exception(ex.Message);
}
} #region ++ Set的多种实现方式
/// <summary>
/// 默认时间
/// </summary>
public static void Set(string depend, string key, object obj)
{
MemcachedHelper.Set(depend, key, obj, _EXP_);
}
/// <summary>
/// 默认Depend和时间
/// </summary>
public static void Set(string key, object obj)
{
MemcachedHelper.Set(_DEPEND_, key, obj, _EXP_);
}
/// <summary>
/// 默认Depend
/// </summary>
public static void Set(string key, object obj, int exp)
{
MemcachedHelper.Set(_DEPEND_, key, obj, exp);
}
/// <summary>
/// 长时间缓存
/// </summary>
public static void SetLong(string depend, string key, object obj)
{
int t = ; //1年 = 10 * 365 * 24 * 60 * 60;
MemcachedHelper.Set(depend, key, obj, t);
}
/// <summary>
/// 长时间默认depend
/// </summary>
public static void SetLong(string key, object obj)
{
int t = ; //365 * 24 * 60 * 60; //1年
MemcachedHelper.Set(_DEPEND_, key, obj, t);
}
public static void SetAllLong(string key, object obj)
{
int t = ; //365 * 24 * 60; //10年
MemcachedHelper.Set(_DEPEND_, key, obj, t);
}
#endregion #endregion #region ** 删除缓存 **
/// <summary>
/// 删除有依赖项的Keys的缓存
/// </summary>
public static void RemoveKeys(string depend, string key)
{
depend = depend.ToLower();
key = key.ToLower(); try
{
//HttpContext.Current.Application.Lock(); //删除缓存
mclient.Remove(key); //删除key
MDictionary_RemoveKeys(depend, key); //HttpContext.Current.Application.UnLock(); }
catch (System.Exception ex)
{
throw new Exception(ex.Message);
}
} /// <summary>
/// 删除默认depend的缓存
/// </summary>
public static void RemoveKeys(string key)
{
RemoveKeys(_DEPEND_, key);
} /// <summary>
/// 删除整个依赖项
/// </summary>
public static void RemoveDepend(string depend)
{
depend = depend.ToLower(); try
{
//获取keys列表
List<string> keysList = MDictionary_GetKeys(depend); //循环删除数据
for (int i = ; i < keysList.Count; i++)
{
string k = keysList[i];
//清空缓存Key
mclient.Remove(k);
////清空字典下的Key
//MDictionary.RemoveKeys(depend, k);
}
////清空字典
//MDictionary_RemoveDepend(depend); }
catch (System.Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion #region --字典管理-- #region ** 字典存取 **
/// <summary>
/// 取出字典
/// </summary>
public static Dictionary<string, List<string>> MDictionary_GetDict()
{
Dictionary<string, List<string>> dict = mclient.Get(_DICT_CACHE_) as Dictionary<string, List<string>>;
if (dict == null)
{
Dictionary<string, List<string>> newDict = new Dictionary<string, List<string>>();
MDictionary_SaveDict(newDict);
return newDict;
}
else
{
return dict;
}
} /// <summary>
/// 存入字典
/// </summary>
public static void MDictionary_SaveDict(Dictionary<string, List<string>> dict)
{
//默认保存360天
mclient.Add(_DICT_CACHE_, dict, HH * * );
} /// <summary>
/// 添加并存入
/// </summary>
public static void MDictionary_AddToDictAndSave(string depend, List<string> listKeys)
{
//取出字典
Dictionary<string, List<string>> dict = MDictionary_GetDict(); //修改或新增字典
if (dict.ContainsKey(depend))
{
dict[depend] = listKeys; //覆盖
}
else
{
dict.Add(depend, listKeys); //新添加
} //存回
MDictionary_SaveDict(dict);
}
#endregion #region ** keys操作 **
/// <summary>
/// 根据depend获取Keys列表
/// </summary>
public static List<string> MDictionary_GetKeys(string depend)
{
depend = depend.ToLower(); Dictionary<string, List<string>> dict = MDictionary_GetDict();
if (dict.ContainsKey(depend))
{
return dict[depend];
}
return new List<string>();
} /// <summary>
/// 添加keys到字典
/// </summary>
public static void MDictionary_AddKeys(string depend, string key)
{
depend = depend.ToLower();
key = key.ToLower(); //添加keys列表
List<string> listKeys = MDictionary_GetKeys(depend);
if (!listKeys.Contains(key))
{
listKeys.Add(key);
//添加并存回字典
MDictionary_AddToDictAndSave(depend, listKeys);
} } /// <summary>
/// 从字典中删除一个Key
/// </summary>
public static void MDictionary_RemoveKeys(string depend, string key)
{
depend = depend.ToLower();
key = key.ToLower(); List<string> listKeys = MDictionary_GetKeys(depend);
if (listKeys.Contains(key))
{
listKeys.Remove(key);
//添加并存回字典
MDictionary_AddToDictAndSave(depend, listKeys);
}
} /// <summary>
/// 删除depend下所有的keys列表
/// </summary>
public static void MDictionary_RemoveDepend(string depend)
{
depend = depend.ToLower(); Dictionary<string, List<string>> dict = MDictionary_GetDict();
if (dict.ContainsKey(depend))
{
dict.Remove(depend);
//存回
MDictionary_SaveDict(dict);
}
}
#endregion #endregion
}
老项目用到depend组,通过组控制组下面所有KEY/VALUE。升级Core改写代码过程中了解到EnyimMemcachedCore相关用法,所以基于博客园Memcached整理了此类。
据说EnyimMemcachedCore开发团队不推荐使用静态类,尽可能地避免使用静态类。
在使用Startup.cs中依赖注入时碰到缓存写入失败,暂未找到原因,以下是我的操作步骤,有清楚的园友请留言告知,谢谢。
第一步:配置文件
"enyimMemcached": {
"Servers": [
{
"Address": "127.0.0.1",
"Port":
}
]
}
第二步:ConfigureServices配置AddEnyimMemcached
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); //memcachedcore1
services.AddEnyimMemcached(options => Configuration.GetSection("enyimMemcached").Bind(options));
}
第三步:Configure方法中使用
app.UseEnyimMemcached();//memcachedcore2
第四步:MVC模式下HomeController测试写入/读取
private IMemcachedClient _memcachedClient;//调用memcachedcore3
public HomeController(IMemcachedClient memcachedClient)
{
_memcachedClient = memcachedClient;//赋值memcachedcore4
}
public IActionResult Index()
{ #region --测试memcachedcore-- MemcachedHelper.Set("memcached", "memcached-core", "memcachedcore" + DateTime.Now, );
string mh = MemcachedHelper.Get("memcached-core").ToString();
ViewData["mhelper"] = mh; //这种方式暂未找到合适赋值,待研究,赋值赋不上。
//删/增/取memcachedcore5
//var cacheKey = "memcachedcore";
////_memcachedClient.Add(cacheKey, "memcachedcore" + DateTime.Now, 60);//此方法赋不上值
////var result = _memcachedClient.Get(cacheKey);
//_memcachedClient.Remove(cacheKey);
//ViewData["memcachedcore"] = result.ToString();
#endregion }
UtilConf读取配置文件操作类
public class UtilConf
{
private static IConfiguration config;
public static IConfiguration Configuration//加载配置文件
{
get
{
if (config != null) return config;
config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
return config;
}
set => config = value;
}
}
2017-12-08 补充
如果多台memcached服务器,服务器时间一定要调整一致。如果不一致就会出现存不上值或过期日期不准确。
Asp.Net Core 2.0 项目实战(5)Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。的更多相关文章
- Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录
1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...
- Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级
1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...
- Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例
本文目录 1. Net下日志记录 2. NLog的使用 2.1 添加nuget引用NLog.Web.AspNetCore 2.2 配置文件设置 2.3 依赖配置及调用 ...
- Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类
本文目录 1. 前沿 2.CacheHelper基于Microsoft.Extensions.Caching.Memory封装 3.XmlHelper快速操作xml文档 4.Serializatio ...
- Asp.Net Core 2.0 项目实战(7)MD5加密、AES&DES对称加解密
本文目录 1. 摘要 2. MD5加密封装 3. AES的加密.解密 4. DES加密/解密 5. 总结 1. 摘要 C#中常用的一些加密和解密方案,如:md5加密.RSA加密与解密和DES加密等, ...
- Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例
本文目录 1. 摘要 2. Redis配置 3. RedisHelper 4.使用实例 5. 总结 1. 摘要 由于內存存取速度远高于磁盘读取的特性,为了程序效率提高性能,通常会把常用的不常变动的数 ...
- Asp.Net Core 2.0 项目实战(4)ADO.NET操作数据库封装、 EF Core操作及实例
Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...
- Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了
Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...
- Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架
Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...
- Asp.Net Core 2.0 项目实战(3)NCMVC角色权限管理前端UI预览及下载
Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...
随机推荐
- C#析构函数与Dispose
有几种不同的操作方式 方式一: namespace ConsoleApp1 { class Test { ~Test()// 析构函数 { ...
- Nginx GZIP 压缩
[ HTTP 开启gzip ] gzip on; // 开启 nginx在线实时压缩数据流: gzip_min_length 1k; // 允许压缩的页面最小字节 gzip_buffers 32k; ...
- oracle锁表
一.锁表的处理 Oracle锁表比较简单,查询锁表的session杀掉就可以了. 1.以下几个为相关表 SELECT * FROM V$LOCK; SELECT * FROM V$SQLAREA; S ...
- centOS7安装Composer
1.进入Composer国内镜像网站文档页查看安装方法: https://docs.phpcomposer.com/00-intro.html 2.在centOS系统中进入特定目录执行以下命令: cd ...
- SVN备份还原
本文是对SVN备份还原的一个简单记录 /*千万不能用VisualSVN Server PowerShell,否则在还原Load的时候会发生错误E140001,具体参考http://stackoverf ...
- 2018-2019-2 网络对抗技术 20165227 Exp2 后门原理与实践
2018-2019-2 网络对抗技术 20165227 Exp2 后门原理与实践 (1)例举你能想到的一个后门进入到你系统中的可能方式? 接收邮件的方式 (2)例举你知道的后门如何启动起来(win及l ...
- spring整合ehcache2.5.2缓存异常-- net.sf.ehcache.CacheException
报错如下: The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcach ...
- 【Python】批量查询-提取站长之家IP批量查询的结果v1.0
0 前言 写报告的时候为了细致性,要把IP地址对应的地区给整理出来.500多条IP地址找出对应地区复制粘贴到报告里整了一个上午. 为了下次更好的完成这项重复性很高的工作,所以写了这个小的脚本. 1 使 ...
- 查看Windows版本号
1.Win+R,在运行中输入:msinfo32 2.在cmd中输入:ver 3.注册表(regedit)中:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows ...
- OpenWrt启动过程分析+添加自启动脚本【转】
一.OpenWrt启动过程分析 转自: http://www.eehello.com/?post=107 总结一下OpenWrt的启动流程:1.CFE->2.linux->3./etc/p ...