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帮助类。的更多相关文章

  1. Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录

    1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...

  2. Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级

    1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...

  3. Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例

    本文目录 1. Net下日志记录 2. NLog的使用     2.1 添加nuget引用NLog.Web.AspNetCore     2.2 配置文件设置     2.3 依赖配置及调用     ...

  4. Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类

    本文目录 1.  前沿 2.CacheHelper基于Microsoft.Extensions.Caching.Memory封装 3.XmlHelper快速操作xml文档 4.Serializatio ...

  5. Asp.Net Core 2.0 项目实战(7)MD5加密、AES&DES对称加解密

    本文目录 1. 摘要 2. MD5加密封装 3. AES的加密.解密 4. DES加密/解密 5. 总结 1.  摘要 C#中常用的一些加密和解密方案,如:md5加密.RSA加密与解密和DES加密等, ...

  6. Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例

    本文目录 1. 摘要 2. Redis配置 3. RedisHelper 4.使用实例 5. 总结 1.  摘要 由于內存存取速度远高于磁盘读取的特性,为了程序效率提高性能,通常会把常用的不常变动的数 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. linux 命令收集 阿里云nginx升级等 查看磁盘空间 版本等

    —————————————————— 查磁盘 df -h此命令直观的呈现出磁盘大小有多少Gdf -hl文件系统 容量 已用 可用 已用% 挂载点/dev/hdb2 75G 75G 0 100% /就是 ...

  2. 使用 CSS3 的 box-sizing 属性设置元素大小包含 border 与 padding

    Ø  默认情况下,内部元素(如:input)的宽度或高度,是不会包含元素的边框和内边距的,这时就需要使用 box-sizing 属性设置该元素. Ø  box-sizing 是 CSS3 的属性,可以 ...

  3. u-boot移植(二)---修改前工作:代码流程分析1

    一.代码执行总体流程图 1.1 代码路径 U-boot.lds (arch\arm\cpu) vectors.S (arch\arm\lib) start.S (arch\arm\cpu\arm920 ...

  4. 微信公众号JSAPI支付-多公众号向同一商户号支付的问题解决

    一.背景 项目提供公众号商城集成,在公众号里进行商品的购买,并与多家公众号合作增加渠道流量. . 二.实现 有关微信公众号.商户号的开通与支付绑定不细说 从背景里可知,我们需要实现多个公众号购买向同一 ...

  5. <video>标签:视频播放器动态设置src

    HTML代码 <div  id="my_div">    <video id="my_video"  width="600" ...

  6. python - class内置方法 doc/module/del(析构方法)/cal 方法

    __doc__ # __doc__ #摘要信息 #这个属性不会继承给子类 class Test(): """这是摘要信息""" pass x ...

  7. 通过Application传递数据

    1:通过Application传递数据 假如有一个Activity A, 跳转到 Activity B ,并需要推荐一些数据,通常的作法是Intent.putExtra() 让Intent携带,或者有 ...

  8. CMake 实践教程

    本篇博客是根据 <<CMake Practice>> 一文编写, 目的有三: 其一: 提取出其中的精要部分; 其二: 对其中不易理解的地方进行简要说明; 其三: 方便后续查找复 ...

  9. 线路板(PCB)制作流程中英文对照表

    线路板(PCB)流程术语中英文对照流程简介:开料--钻孔--干膜制程--压合--减铜--电镀--塞孔--防焊(绿漆/绿油) --镀金--喷锡--成型--开短路测试--终检--雷射钻孔A. 开料( Cu ...

  10. 【转】Python之列表生成式、生成器、可迭代对象与迭代器

    [转]Python之列表生成式.生成器.可迭代对象与迭代器 本节内容 语法糖的概念 列表生成式 生成器(Generator) 可迭代对象(Iterable) 迭代器(Iterator) Iterabl ...