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. 《Two Dozen Short Lessons in Haskell》所有习题的索引

    <Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...

  2. jquery 兼容的滚轮事件

    // jquery 兼容的滚轮事件 $(document).on("mousewheel DOMMouseScroll", function (e) { ? : -)) || // ...

  3. aop(Aspect Oriented Programming)面向切面编程

    AOP,一个oop的后传,面向切面,一个基于代理模式的机制. 举例子把:说原理很难理解, 吃饭:谁都得吃饭,一个老师类,有吃饭的方法.一个学生类,也得有吃饭的方法.是不是都得实现这个方法?构建一个pe ...

  4. JavaScript之数值计算

    //两等长数组对应元素之间做减法运算[可拓展:基本运算(+/-*//)] function array_dif(length,arrayA,arrayB){ var array = new Array ...

  5. react框架的状态管理

    安装: cnpm install --save redux cnpm install --save react-redux   安装好后导入模块内容: impor {createStore} from ...

  6. 以前的 Delphi版本

                    Delphi 1 Delphi 2 Delphi 3 Delphi 4 Delphi 5 Delphi 6 Delphi 7 Delphi 8 Delphi 2005

  7. 【Python】【辅助程序】练手小程序:记录外网动态IP地址

    练手小程序 程序作用:对IP实时记录: 1.定时获取外网IP,存储在本地文件中: 编写思路: 1)收集获取外网的API接口       http://bbs.125.la/thread-1383897 ...

  8. freeRTOS中文实用教程3--中断管理之计数信号量

    1.前言 在中断不频繁的系统中,使用二值信号量没有问题,但是中断频繁发生时,则会有中断丢失的问题. 因为中断发生时延迟任务执行,延迟任务执行的过程中,如果又来了两次中断,则只会处理第一次,第二次将会丢 ...

  9. 利用autocomplete.js实现仿百度搜索效果(ajax动态获取后端[C#]数据)

    实现功能描述: 1.实现搜索框的智能提示 2.第二次浏览器缓存结果 3.实现仿百度搜索 <!DOCTYPE html> <html xmlns="http://www.w3 ...

  10. linux网络设备驱动

    Linux网络设备驱动 Linux网络驱动程序的体系结构可划分为4个层次.Linux内核源代码中提供了网络设备接口及以网络子系统的上层的代码,移植特定网络硬件的驱动程序的主要工作就是完成设备驱动功能层 ...