96 lines
1.6 KiB
C#
96 lines
1.6 KiB
C#
using MAX.Core.Caches;
|
|
using Store.Management.Framework.Helpers;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Store.Management.Framework.Caches;
|
|
|
|
/// <summary>
|
|
/// 图片缓存//
|
|
/// </summary>
|
|
internal class ImageCache : SingleInstanceCache<ImageCache, StoreImageBase>
|
|
{
|
|
|
|
private ImageCache()
|
|
{
|
|
|
|
}
|
|
public override void Clear()
|
|
{
|
|
base.Clear();
|
|
GC.Collect();
|
|
}
|
|
|
|
|
|
|
|
public byte[] GetBytes(string key)
|
|
{
|
|
var item = Get(key);
|
|
if (item != null)
|
|
{
|
|
return item.ImgByte;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public string GetUploadUrl(string key)
|
|
{
|
|
var item = Get(key);
|
|
if (item != null)
|
|
{
|
|
return item.UploadImgUrl;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 添加缓存
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="file">图片流</param>
|
|
/// <returns></returns>
|
|
public bool TryAdd(string key, byte[] file)
|
|
{
|
|
var item = Get(key);
|
|
if (item == null)
|
|
{
|
|
item = new StoreImageBase();
|
|
item.ImgUrl = key;
|
|
}
|
|
item.ImgByte = file;
|
|
return base.TryAdd(key, item);
|
|
}
|
|
/// <summary>
|
|
/// 添加缓存
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="uploadUrl">图片上传后服务器地址</param>
|
|
/// <returns></returns>
|
|
public bool TryAdd(string key, string uploadUrl)
|
|
{
|
|
var item = Get(key);
|
|
if (item == null)
|
|
{
|
|
item = new StoreImageBase();
|
|
item.ImgUrl = key;
|
|
}
|
|
if (item.ImgByte == null)
|
|
{
|
|
item.UploadImgUrl = uploadUrl;
|
|
}
|
|
return base.TryAdd(key, item);
|
|
}
|
|
|
|
public bool TryAdd(StoreImageBase img)
|
|
{
|
|
return base.TryAdd(img.ImgUrl, img);
|
|
}
|
|
}
|