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服务器,服务器时间一定要调整一致。如果不一致就会出现存不上值或过期日期不准确。

NET Core2.0 Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。的更多相关文章

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

  2. 记一次Docker中部署Asp.Net Core 3.0的踩坑过程

    最近公司打算重构目前直销报单系统到微信小程序中,目前的系统只能在PC上面使用,这两年也搞过App端,但是由于人员流动和公司架构调整最后都不了了之,只留下一堆写了一半的接口.以前的接口依然是使用Asp. ...

  3. 问题定位 | Peronca Xtrabackup 8.0近日踩坑总结 - xtrabackup 2.4和8.0区别

    目录 前言 适配过程中遇到的坑 xtrabackup 2.4和8.0区别 问题定位 坑一:MySQL 8.0 + Semi-Sync 重建问题 坑二:MySQL 8.0 + Group-Replica ...

  4. 在IIS上部署 .Net Core 3.0 项目踩坑实录

    在IIS上部署 .Net Core 3.0 项目的主要流程有: 安装并启用IIS 安装AspNetCoreModuleV2 添加.配置网站 设置应用程序池 通过VS发布 一.安装并启用IIS: 安装了 ...

  5. .net core 2.2.0 SOAP踩坑

    首先确认下面几个程序集是最新版本: <PackageReference Include="System.ServiceModel.Http" Version="4. ...

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

  7. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-1目录

    Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...

  8. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-7项目缓冲方案( Redis)

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

  9. lombok踩坑与思考

    虽然接触到lombok已经有很长时间,但是大量使用lombok以减少代码编写还是在新团队编写新代码维护老代码中遇到的. 我个人并不主张使用lombok,其带来的代价足以抵消其便利,但是由于团队编码风格 ...

随机推荐

  1. dfs练习

    不给提示,练习. 题意: 蒜头的数学实在是太差了,于是老师把他关到小黑屋让他闭门修炼.老师跟他一张纸,上面一排写着1, 2, 3...N这N个数,中间用空白分隔.老师让他在空白处填上加号或者减号.他让 ...

  2. HDU5992 - Finding Hotels

    原题链接 Description 给出个二维平面上的点,每个点有权值.次询问,求所有权值小于等于的点中,距离坐标的欧几里得距离最小的点.如果有多个满足条件的点,输出最靠前的一个. Solution 拿 ...

  3. mysql常用脚本及命令记录

    mysql导出用户权限 mysql中直接通过授权即可使用对应用户,不必使用创建用户命令(如CREATE USER 'xxx'@'%' IDENTIFIED BY 'XXX';)先建用户再授权. 方法一 ...

  4. python>oop

    oop即Object Oriented programming,面向对象编程 何为编程:特定的语法+数据结构+算法 编程范式就是从经验中总结的套路: 面向过程编程 适用于简单的脚本,一次任务是极好的, ...

  5. nginx笔记6-总结

    1.轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除.2.weight指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况.3.ip_ ...

  6. (2018干货系列一)最新Java学习路线整合

    怎么学Java Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征. 话不多说,直接上干货: ...

  7. 6.2 PowerPC处理器如何处理MSI中断请求

    PowerPC处理器使用OpenPIC中断控制器或者MPIC中断控制器,处理外部中断请求.其中MPIC中断控制器基于OpenPIC中断控制器,但是作出了许多增强,目前Freescale新推出的Powe ...

  8. 图解MBR分区无损转换GPT分区+UEFI引导安装WIN8.1

    确定你的主板支持UEFI引导.1,前期准备,WIN8.1原版系统一份(坛子里很多,自己下载个),U盘2个其中大于4G一个(最好 准备两个U盘)2,大家都知道WIN8系统只支持GPT分区,传统的MBR分 ...

  9. PCI设备内存操作函数总结

    1.  ExAllocatePool() 函数说明: ExAllocatePool allocates pool memory of the specified type and returns a ...

  10. Linux显示包含全部的文件系统

    Linux显示包含全部的文件系统 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ df -a 文件系统 1K-blocks 已用 可用 已用% 挂载点 /dev ...