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. js设定延迟时间的函数

    1.如果想要在执行一个js函数之前延迟一段时间应该怎么做? 答:"setTimeout('update()',1000);" 其中update()函数就是延迟后执行的函数,后面的时 ...

  2. Mongodb 副本集

    mongodb主从模式就是一个 单副本的应用:没有很好的扩展性和容错性: 副本集的多个副本保证了容错性:主服务器负责整个副本集的读写,副本集定时同步数据:主节点挂掉:副本集会自动选举一个主的服务器: ...

  3. UE4中Bebavior Tree中Delay及其后面代码失效的原因

    具体原因是因为节点的执行过程中,该节点及其父节点的Decorator条件不满足,而节点又受到flow control的影响,导致中途强制结束了Task节点的执行,具体如下. UE4中的Behavior ...

  4. MySQL5.7使用错误解决:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)【取消或重设root密码】

    解决方法: 最简单方法: ⑴打开mysql中的my.ini(如果没有就将my-default.ini复制一份,并修改为my.ini): ⑵在[mysqld]下面空白行直接添加skip-grant-ta ...

  5. HTTP状态码解析

    HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码.它由 RFC 2616 规范定义的,并得到RFC 2518.RFC 2817.RFC 2295.R ...

  6. android greenDao使用

    github:https://github.com/greenrobot/greenDAO 基本使用:https://toutiao.io/posts/yg1kyu/preview https://b ...

  7. java linux ftp问题

    java写的ftp上传类,本地测试环境可以用,阿里云服务器不可用,两者系统均为centos.经过测试,发现appche的ftpclient类不可用,换成sun的ftpclient可以使用.

  8. myeclipse的web项目导入到eclipse中

    环境组成: java8 eclipse4.4.2 for javaee tomcat 7.0.61 1.导入myeclipse项目 2.设置JDK环境 3.将导入的项目修改为web项目 将myecli ...

  9. 配置vCenter Server Appliance 6.7

    =============================================== 2019/4/14_第1次修改                       ccb_warlock == ...

  10. Java Map 键值对排序 按key排序和按Value排序

    一.理论准备 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black tre ...