CSharp_DistributedCache_Simple
一、WinForm版(EnyimMemcached)
1、Memcache示例
2、引用Negut包
3、MemcachedHelper
/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:Memcached帮助类(使用示例)
*│ 作 者:执笔小白;主要参考文章
*│ 版 本:1.0
*│ 创建时间:2024-1-10 14:08:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Util.MemoryCacheDome
*│ 类 名:MemcachedHelper
*└──────────────────────────────────────────────────────────────┘
*/
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using System;
using System.Windows.Forms;
namespace MemoryCacheDome
{
/// <summary>
/// MemoryCached帮助类
/// </summary>
public static class MemcachedHelper
{
#region 变量
/// <summary>
/// 定义一个静态MemcachedClient客户端
/// </summary>
private static MemcachedClient mclient;
#endregion 变量
/// <summary>
/// 初始化Memcached客户端
/// </summary>
public static MemcachedClient Init(string ip, int port, string zone, string userName, string password, int maxPoolSize = 0, int minPoolSize = 0)
{
//初始化缓存
MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration();
#region 配置
memConfig.Protocol = MemcachedProtocol.Binary; // 二进制流
if (minPoolSize != 0)
{
memConfig.SocketPool.MinPoolSize = minPoolSize; // 最小socket pool
}
if (maxPoolSize != 0)
{
memConfig.SocketPool.MaxPoolSize = maxPoolSize; // 最大socket pool
}
memConfig.AddServer(ip, port); // Memcached的地址
if (!string.IsNullOrEmpty(userName))
{
memConfig.Authentication.Type = typeof(PlainTextAuthenticator); // Memcached的加密方式
memConfig.Authentication.Parameters["zone"] = zone; // zone区域
memConfig.Authentication.Parameters["userName"] = userName; // Memcached的用户
memConfig.Authentication.Parameters["password"] = password; // Memcached的密码
}
#endregion 配置
mclient = new MemcachedClient(memConfig);
return mclient;
}
public static void Dispose()=> mclient.Dispose();
#region 常用方法
/// <summary>
/// 判断数据是否存在
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool TryGet<T>(string groupName, string key, out object value) => mclient.TryGet(groupName + "_" + key, out value);
/// <summary>
/// 获取数据
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
/// <param name="key">键</param>
/// <returns></returns>
public static T Get<T>(string groupName, string key) => mclient.Get<T>(groupName + "_" + key);
/// <summary>
/// 创建缓存对象并添加数据
/// </summary>
/// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="expiry">过期时间</param>
/// <returns>返回是否添加成功</returns>
public static bool Store(string groupName, string key, object value, DateTime expiry)
=> mclient.Store(Enyim.Caching.Memcached.StoreMode.Set, groupName + "_" + key, value, expiry);
/// <summary>
/// 追加数据到指定缓存对象;只支持‘ArraySegment<byte>类型’
/// </summary>
/// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
/// <param name="key">键(指定缓存对象)</param>
/// <param name="value">值;ArraySegment<byte>类型</param>
/// <returns>返回是否追加成功</returns>
public static bool Append(string groupName, string key, ArraySegment<byte> value) => mclient.Append(groupName + "_" + key, value);
/// <summary>
/// 替换指定数据
/// </summary>
/// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="expiry">过期时间</param>
public static void Replace(string groupName, string key, object value, DateTime expiry)
=> mclient.Store(Enyim.Caching.Memcached.StoreMode.Replace, groupName + "_" + key, value, expiry);
/// <summary>
/// 移除数据
/// </summary>
/// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
/// <param name="key">键</param>
/// <returns></returns>
public static bool Remove(string groupName, string key) => mclient.Remove(groupName + "_" + key);
/// <summary>
/// 清除所有数据
/// </summary>
public static void FlushAll() => mclient.FlushAll();
#endregion 常用方法
/// <summary>
/// 统计-不常用
/// </summary>
public static void Stats() => mclient.Stats();
}
}
二、Web应用Net7版(EnyimMemcachedCore)
C#-缓存(二)_分布式缓存DistributedCache
作者:꧁执笔小白꧂