初始化
This commit is contained in:
271
Face.Services/Cache/BaseCacheHelp.cs
Normal file
271
Face.Services/Cache/BaseCacheHelp.cs
Normal file
@@ -0,0 +1,271 @@
|
||||
using Face.Services.Enums;
|
||||
using Face.Services.Extensions;
|
||||
using Face.Services.Tool;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Services.Cache
|
||||
{
|
||||
public class BaseCacheHelp : BaseCacheHelpRedis
|
||||
{
|
||||
}
|
||||
|
||||
#region 缓存管理-MVC
|
||||
/// <summary>
|
||||
/// 缓存管理-MVC内核
|
||||
/// </summary>
|
||||
public abstract class BaseCacheHelpMVC
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取设置-string
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="setAcrion">委托方法</param>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="cacheTimeType">超时设置</param>
|
||||
/// <param name="cacheTime">超时设置</param>
|
||||
/// <returns>返回泛型实例</returns>
|
||||
public static T GetCache<T>(string key, Func<T> setAcrion, CacheTimeType? cacheTimeType = null, int? cacheTime = null)
|
||||
{
|
||||
if (CacheExtensions.CheckCache(key))
|
||||
{
|
||||
return CacheExtensions.GetCache<T>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
T model = setAcrion();
|
||||
if (model != null)
|
||||
{
|
||||
if (cacheTimeType != null && cacheTime != null)
|
||||
{
|
||||
CacheExtensions.SetCache(key, model, (CacheTimeType)cacheTimeType, (int)cacheTime);
|
||||
}
|
||||
}
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置参数 string
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="setAcrion">委托方法</param>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="cacheTimeType">超时设置</param>
|
||||
/// <param name="cacheTime">超时设置</param>
|
||||
public static void SetCache<T>(string key, Func<T> setAcrion, CacheTimeType? cacheTimeType = null, int? cacheTime = null)
|
||||
{
|
||||
T model = setAcrion();
|
||||
if (model != null)
|
||||
{
|
||||
if (cacheTimeType != null && cacheTime != null)
|
||||
{
|
||||
CacheExtensions.SetCache(key, model, (CacheTimeType)cacheTimeType, (int)cacheTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 string 失败返回 null
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <returns>返回泛型实例</returns>
|
||||
public static T GetVaue<T>(string key)
|
||||
{
|
||||
if (CacheExtensions.CheckCache(key))
|
||||
{
|
||||
return CacheExtensions.GetCache<T>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
public static void ClearCache(string key)
|
||||
{
|
||||
if (CacheExtensions.CheckCache(key))
|
||||
{
|
||||
CacheExtensions.ClearCache(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region 缓存管理
|
||||
/// <summary>
|
||||
/// 设置缓存
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
public static T GetCacheHelp<T>(string key, Func<T> action)
|
||||
{
|
||||
if (CacheExtensions.CheckCache(key))
|
||||
{
|
||||
return CacheExtensions.GetCache<T>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
var model = action();
|
||||
CacheExtensions.SetCache(key, model);
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 缓存管理-Redis 基于Redis-string
|
||||
/// <summary>
|
||||
/// 缓存管理-Redis内核 基于Redis-string
|
||||
/// </summary>
|
||||
public abstract class BaseCacheHelpRedis
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取设置-string
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="setAcrion">委托方法</param>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="expiry">超时设置</param>
|
||||
/// <returns>返回泛型实例</returns>
|
||||
public static T GetCache<T>(string key, Func<T> setAcrion, TimeSpan? expiry = default(TimeSpan?))
|
||||
{
|
||||
if (RedisHelper.CheckKey(key))
|
||||
{
|
||||
return RedisHelper.StringGet<T>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
T model = setAcrion();
|
||||
if (model != null)
|
||||
{
|
||||
RedisHelper.StringSet(key, model, expiry);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置参数 string
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="setAcrion">委托方法</param>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="expiry">超时设置</param>
|
||||
public static void SetCache<T>(string key, Func<T> setAcrion, TimeSpan? expiry = default(TimeSpan?))
|
||||
{
|
||||
T model = setAcrion();
|
||||
if (model != null)
|
||||
{
|
||||
RedisHelper.StringSet(key, model, expiry);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 string 失败返回 null
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <returns>返回泛型实例</returns>
|
||||
public static T GetVaue<T>(string key)
|
||||
{
|
||||
if (RedisHelper.CheckKey(key))
|
||||
{
|
||||
return RedisHelper.StringGet<T>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
public static void ClearCache(string key)
|
||||
{
|
||||
if (RedisHelper.CheckKey(key))
|
||||
{
|
||||
RedisHelper.RemoveKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取哈希缓存,没有则添加
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="sid">键</param>
|
||||
/// <param name="setAcrion">委托方法</param>
|
||||
/// <returns></returns>
|
||||
public static T GetHashCache<T>(string key, string sid, Func<T> setAcrion)
|
||||
{
|
||||
if (RedisHelper.HashExists(key, sid))
|
||||
{
|
||||
return RedisHelper.HashGet<T>(key, sid);
|
||||
}
|
||||
else
|
||||
{
|
||||
T model = setAcrion();
|
||||
if (model != null)
|
||||
{
|
||||
RedisHelper.HashSet(key, sid, model);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入哈希缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="sid">键</param>
|
||||
/// <param name="model">实体</param>
|
||||
public static void SetHashCache<T>(string key, string sid, T model)
|
||||
{
|
||||
RedisHelper.HashSet(key, sid, model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入哈希缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="dic">字典</param>
|
||||
public static void SetHashCache<T>(string key, Dictionary<string, T> dic)
|
||||
{
|
||||
RedisHelper.HashSet(key, dic);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取哈希缓存字典
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<string, T> GetHashAllCache<T>(string key)
|
||||
{
|
||||
return RedisHelper.HashGetAll<T>(key);
|
||||
}
|
||||
|
||||
public static bool CheckKey(string key)
|
||||
{
|
||||
return RedisHelper.CheckKey(key);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
320
Face.Services/Cache/CacheHelp.cs
Normal file
320
Face.Services/Cache/CacheHelp.cs
Normal file
@@ -0,0 +1,320 @@
|
||||
using Face.Domain.Entities;
|
||||
using Face.Services.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Face.Domain.ViewModels;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using Face.Services.Manager;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Face.Services.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存
|
||||
/// </summary>
|
||||
public static class CacheHelp
|
||||
{
|
||||
|
||||
|
||||
|
||||
private static readonly string FaceListKsy = "sysFaceList";//人脸机列表
|
||||
private static readonly string HotelListKsy = "sysHotelslList";//酒店列表
|
||||
private static readonly string RoomCustomerKsy = "sysRoomCustomerList";//入住客人信息列表
|
||||
private static readonly string RoomCheckKey = "sysRoomCheckKeyList";//开放入住信息
|
||||
private static readonly string HotelGroupKey = "sysHotelGroupList";
|
||||
private static readonly string sysUserOperationKey = "UserOperation_";//用户当前选择库
|
||||
private static readonly string RoomGroupKey = "sysRoomGroupList";
|
||||
private static readonly string testinfo = "systest";
|
||||
|
||||
#region Hash缓存前缀
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 获取写入酒店列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<Hotels> GetHotelslist()
|
||||
{
|
||||
return BaseCacheHelp.GetCache(HotelListKsy, () =>
|
||||
{
|
||||
var list = new List<Hotels>();
|
||||
|
||||
list = SqlSugarBase.authoriydb.Queryable<Hotels>().ToList();
|
||||
|
||||
return list;
|
||||
});
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除酒店缓存
|
||||
/// </summary>
|
||||
public static void ClearHotelList()
|
||||
{
|
||||
|
||||
BaseCacheHelp.ClearCache(HotelListKsy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户当前选择库
|
||||
/// </summary>
|
||||
/// <param name="userName"></param>
|
||||
/// <param name="dbID"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetUserOperation(string userName, int dbID = 0)
|
||||
{
|
||||
if (dbID == 0)
|
||||
{
|
||||
return BaseCacheHelp.GetCache(sysUserOperationKey + userName, () =>
|
||||
{
|
||||
return dbID;
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
BaseCacheHelp.SetCache(sysUserOperationKey + userName, () =>
|
||||
{
|
||||
return dbID;
|
||||
});
|
||||
return dbID;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取写入人脸机列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<DeviceManage> GetFaceList()
|
||||
{
|
||||
|
||||
return BaseCacheHelp.GetCache(FaceListKsy, () =>
|
||||
{
|
||||
var list = new List<DeviceManage>();
|
||||
using (SqlSugarClient db = SqlSugarBase.GesmartDb())
|
||||
{
|
||||
list = db.Queryable<DeviceManage>().ToList();
|
||||
}
|
||||
return list;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取总数
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int Geiface(string name,int i)
|
||||
{
|
||||
|
||||
return BaseCacheHelp.GetCache(name, () =>
|
||||
{
|
||||
return i;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 删除人脸机缓存
|
||||
/// </summary>
|
||||
public static void ClearFaceList()
|
||||
{
|
||||
BaseCacheHelp.ClearCache(FaceListKsy);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 删除入住客人信息
|
||||
/// </summary>
|
||||
public static void ClearRoomCustomer()
|
||||
{
|
||||
BaseCacheHelp.ClearCache(RoomCustomerKsy);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取开房入住信息列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<Hosts> GetRoomCheck()
|
||||
{
|
||||
return BaseCacheHelp.GetCache(RoomCheckKey, () =>
|
||||
{
|
||||
var list = new List<Hosts>();
|
||||
|
||||
list =SqlSugarBase.authoriydb.Queryable<Hosts>().ToList();
|
||||
|
||||
return list;
|
||||
});
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除开房入住信息
|
||||
/// </summary>
|
||||
public static void ClearRoomCheck()
|
||||
{
|
||||
BaseCacheHelp.ClearCache(RoomCheckKey);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取酒店项目信息列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<HotelGroups> GetHotelGroup()
|
||||
{
|
||||
return BaseCacheHelp.GetCache(HotelGroupKey, () =>
|
||||
{
|
||||
var list = new List<HotelGroups>();
|
||||
|
||||
list = SqlSugarBase.authoriydb.Queryable<HotelGroups>().ToList();
|
||||
|
||||
return list;
|
||||
});
|
||||
}
|
||||
//从数据库查询
|
||||
public static List<HotelGroups> GetHotelGroup2()
|
||||
{
|
||||
var list = new List<HotelGroups>();
|
||||
|
||||
list = SqlSugarBase.authoriydb.Queryable<HotelGroups>().ToList();
|
||||
|
||||
return list;
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除酒店项目信息
|
||||
/// </summary>
|
||||
public static void ClearHotelGroup()
|
||||
{
|
||||
BaseCacheHelp.ClearCache(HotelGroupKey);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取房间号信息列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<Hosts> GetRoomGroup()
|
||||
{
|
||||
return BaseCacheHelp.GetCache(RoomGroupKey, () =>
|
||||
{
|
||||
var list = new List<Hosts>();
|
||||
|
||||
list = SqlSugarBase.authoriydb.Queryable<Hosts>().ToList();
|
||||
|
||||
return list;
|
||||
});
|
||||
}
|
||||
public static List<Interfacefield> GetInInfoList()
|
||||
{
|
||||
var res =BaseCacheHelp.GetCache(testinfo, () =>
|
||||
{
|
||||
return new List<Interfacefield>();
|
||||
});
|
||||
//foreach (var info in res)
|
||||
//{
|
||||
// //已经过期的
|
||||
// if( DateTime.Now - info.CheckTime.AddHours(1) > TimeSpan.FromSeconds(0))
|
||||
// {
|
||||
// OutClass oc = new OutClass
|
||||
// {
|
||||
// HotelCode = info.HotelCode,
|
||||
// roomid = info.roomid,
|
||||
// checkOutTime = DateTime.Now
|
||||
|
||||
// };
|
||||
// outinfo(oc);
|
||||
// continue;
|
||||
// }
|
||||
// //未过期的
|
||||
// var time = info.CheckTime.AddMinutes(5) - DateTime.Now;
|
||||
// Task.Factory.StartNew(async () => {
|
||||
// await Task.Delay(time);
|
||||
// OutClass oc = new OutClass
|
||||
// {
|
||||
// HotelCode = info.HotelCode,
|
||||
// roomid = info.roomid,
|
||||
// checkOutTime = DateTime.Now
|
||||
|
||||
// };
|
||||
// outinfo(oc);
|
||||
// });
|
||||
//}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void SetInInfoList(Interfacefield info)
|
||||
{
|
||||
|
||||
var time = info.CheckTime.AddMinutes(3) - DateTime.Now ;
|
||||
Task.Factory.StartNew( ()=> {
|
||||
Task.Delay(time).Wait();
|
||||
OutClass oc = new OutClass
|
||||
{
|
||||
HotelCode = info.HotelCode,
|
||||
roomid = info.roomid,
|
||||
checkOutTime = DateTime.Now
|
||||
|
||||
};
|
||||
outinfo(oc);
|
||||
});
|
||||
BaseCacheHelp.SetCache(testinfo,new Func<List<Interfacefield>>(() =>
|
||||
{
|
||||
var data = GetInInfoList();
|
||||
data.Add(info);
|
||||
data = data.Where(x => DateTime.Now - x.CheckTime <= TimeSpan.FromHours(3)).ToList();
|
||||
return data;
|
||||
}));
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除房间信息
|
||||
/// </summary>
|
||||
public static void ClearRoomGroup()
|
||||
{
|
||||
BaseCacheHelp.ClearCache(RoomGroupKey);
|
||||
}
|
||||
|
||||
public static bool outinfo(OutClass po)//退房测试
|
||||
{
|
||||
try
|
||||
{
|
||||
string url = "http://face.blv-oa.com/Interface/CheckOut";
|
||||
//InterfaceController li = new InterfaceController();
|
||||
//li.CheckOut("abc", JsonConvert.SerializeObject(
|
||||
// po
|
||||
// ));
|
||||
string postData = $"key=abc&data={JsonConvert.SerializeObject(po)}";
|
||||
WebRequest request = WebRequest.Create(url);
|
||||
request.Method = "Post";
|
||||
request.ContentType = "application/x-www-form-urlencoded";
|
||||
StreamWriter sw = new StreamWriter(request.GetRequestStream());
|
||||
sw.Write(postData);
|
||||
sw.Flush();
|
||||
|
||||
WebResponse response = request.GetResponse();
|
||||
Stream s = response.GetResponseStream();
|
||||
StreamReader sr = new StreamReader(s, Encoding.GetEncoding("UTF-8"));
|
||||
string Show = sr.ReadToEnd();
|
||||
ReturnResult info = JsonConvert.DeserializeObject<ReturnResult>(Show);//josn转换实体类
|
||||
sw.Dispose();
|
||||
sw.Close();
|
||||
sr.Dispose();
|
||||
sr.Close();
|
||||
s.Dispose();
|
||||
s.Close();
|
||||
return info.Status == 200;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
public class OutClass
|
||||
{
|
||||
public string HotelCode { get; set; }
|
||||
public string roomid { get; set; }
|
||||
|
||||
public DateTime checkOutTime { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user