You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.0 KiB

2 years ago
/*----------------------------------------------------------------
Copyright (C) 2023 Senparc
MemcachedContainerStrategy.cs
Memcached
Senparc - 20160308
Senparc - 20160808
v0.0.2 ItemCollection 使ContainerBag
Senparc - 20160812
v0.0.3 Container
Senparc - 20160812
v0.0.5 ServerList
Senparc - 20170205
v0.2.0
----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Senparc.CO2NET.Cache;
using Senparc.CO2NET.Cache.Memcached;
using Senparc.Weixin.Containers;
#if !NET462
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
#endif
namespace Senparc.Weixin.Cache.Memcached
{
public class MemcachedContainerCacheStrategy : BaseContainerCacheStrategy
{
#region IDomainExtensionCacheStrategy 成员
public override ICacheStrategyDomain CacheStrategyDomain { get { return ContainerCacheStrategyDomain.Instance; } }
#endregion
#region 单例
/// <summary>
/// MemcachedContainerCacheStrategy 的构造函数
/// </summary>
MemcachedContainerCacheStrategy()
{
//使用底层缓存策略
BaseCacheStrategy = () => MemcachedObjectCacheStrategy.Instance;
//向底层缓存注册当前缓存策略
base.RegisterCacheStrategyDomain(this);
}
//静态LocalCacheStrategy
public static IContainerCacheStrategy Instance
{
get
{
return Nested.instance;//返回Nested类中的静态成员instance
}
}
class Nested
{
static Nested()
{
}
//将instance设为一个初始化的LocalCacheStrategy新实例
internal static readonly MemcachedContainerCacheStrategy instance = new MemcachedContainerCacheStrategy();
}
#endregion
#region IContainerCacheStrategy 成员
public override IDictionary<string, TBag> GetAll<TBag>()
{
throw new NotImplementedException();
}
public override void UpdateContainerBag(string key, IBaseContainerBag containerBag, TimeSpan? expiry = null, bool isFullKey = false)
{
var baseCacheStrategy = BaseCacheStrategy();
object value;
if ((baseCacheStrategy as MemcachedObjectCacheStrategy).TryGet(key, out value))
{
baseCacheStrategy.Update(key, containerBag, expiry, isFullKey);
}
}
#region 异步方法
/// <summary>
/// 【异步方法】获取所有 Bag 对象
/// </summary>
/// <typeparam name="TBag"></typeparam>
/// <returns></returns>
public override async Task<IDictionary<string, TBag>> GetAllAsync<TBag>()
{
throw new NotImplementedException();
}
public override async Task UpdateContainerBagAsync(string key, IBaseContainerBag bag, TimeSpan? expiry = null, bool isFullKey = false)
{
var baseCacheStrategy = BaseCacheStrategy();
object value;
if ((baseCacheStrategy as MemcachedObjectCacheStrategy).TryGet(key, out value))
{
await baseCacheStrategy.UpdateAsync(key, bag, expiry, isFullKey);
}
//Memcached 组件没有提供对应 TryGet() 的异步方法,所以也可以考虑使用 Task.Factory 完成异步
//await Task.Factory.StartNew(() => UpdateContainerBag(key, bag, expiry, isFullKey)).ConfigureAwait(false);
}
#endregion
#endregion
}
}