v1.0.3
This commit is contained in:
@@ -30,7 +30,7 @@ namespace CC.Yi.API.Controllers
|
||||
public IActionResult GetTest()//查
|
||||
{
|
||||
var data = _studentBll.GetAllEntities().ToList();
|
||||
return Content(Common.JsonFactory.JsonToString(data));
|
||||
return Content(Common.JsonHelper.JsonToString(data));
|
||||
}
|
||||
[HttpGet]
|
||||
public IActionResult AddTest()//增
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Autofac;
|
||||
using Autofac.Extras.DynamicProxy;
|
||||
using CC.Yi.BLL;
|
||||
using CC.Yi.Common.Cache;
|
||||
using CC.Yi.Common.Castle;
|
||||
using CC.Yi.DAL;
|
||||
using CC.Yi.IBLL;
|
||||
@@ -51,6 +52,11 @@ namespace CC.Yi.API
|
||||
//<2F><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>Autofac
|
||||
//services.AddScoped(typeof(IBaseDal<>), typeof(BaseDal<>));
|
||||
//services.AddScoped(typeof(IstudentBll), typeof(studentBll));
|
||||
services.AddSingleton(typeof(ICacheWriter), new RedisCacheService(new Microsoft.Extensions.Caching.Redis.RedisCacheOptions()
|
||||
{
|
||||
Configuration = Configuration.GetSection("Cache.ConnectionString").Value,
|
||||
InstanceName = Configuration.GetSection("Cache.InstanceName").Value
|
||||
}));
|
||||
}
|
||||
|
||||
//<2F><>̬ <20><><EFBFBD><EFBFBD>AOP˼<50><CBBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2> Autofac
|
||||
@@ -59,7 +65,6 @@ namespace CC.Yi.API
|
||||
builder.RegisterType(typeof(CustomAutofacAop));
|
||||
builder.RegisterGeneric(typeof(BaseDal<>)).As(typeof(IBaseDal<>)) ;
|
||||
builder.RegisterType<studentBll>().As<IstudentBll>().EnableInterfaceInterceptors();//<2F><>ʾע<CABE><D7A2>ǰ<EFBFBD><C7B0>Ҫִ<D2AA><D6B4>Castle
|
||||
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Cache": {
|
||||
"InstanceName": "Redis",
|
||||
"ConnectionString": "127.0.0.1:12345,password=123456"
|
||||
},
|
||||
"ConnectionStringBySQL": "server=.;Database=cctest;UId=sa;PWD=Qz52013142020.",
|
||||
"ConnectionStringByMySQL": "Data Source=.;Database=cctest;User ID=root;Password=Qz52013142020.;pooling=true;port=3306;sslmode=none;CharSet=utf8;"
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
@@ -7,6 +7,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="6.0.0" />
|
||||
<PackageReference Include="Castle.Core" Version="4.4.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
53
CC.Yi.Common/Cache/CacheHelper.cs
Normal file
53
CC.Yi.Common/Cache/CacheHelper.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Autofac;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CC.Yi.Common.Cache
|
||||
{
|
||||
public class CacheHelper
|
||||
{
|
||||
public static ICacheWriter CacheWriter { get; set; }
|
||||
static CacheHelper()
|
||||
{
|
||||
|
||||
//这里存在一些问题
|
||||
ContainerBuilder containerBuilder = new ContainerBuilder();
|
||||
IContainer container = containerBuilder.Build();
|
||||
CacheHelper.CacheWriter = container.Resolve<ICacheWriter>();
|
||||
}
|
||||
|
||||
public static void AddCache(string key, string value, int expDate)
|
||||
{
|
||||
CacheWriter.Add(key, value, expDate);
|
||||
}
|
||||
|
||||
//没有过期参数,表示用不过期
|
||||
public static void AddCache(string key, string value)
|
||||
{
|
||||
CacheWriter.Add(key, value);
|
||||
}
|
||||
|
||||
public static object GetCache(string key)
|
||||
{
|
||||
return CacheWriter.Get(key);
|
||||
}
|
||||
public static void SetCache(string key, string value, int expDate)
|
||||
{
|
||||
|
||||
CacheWriter.Replace(key, value, expDate);
|
||||
}
|
||||
|
||||
//没有过期参数,表示用不过期
|
||||
public static void SetCache(string key, string value)
|
||||
{
|
||||
CacheWriter.Replace(key, value);
|
||||
}
|
||||
|
||||
public static void Remove(string key)
|
||||
{
|
||||
CacheWriter.Remove(key);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
31
CC.Yi.Common/Cache/ICacheWriter.cs
Normal file
31
CC.Yi.Common/Cache/ICacheWriter.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.Yi.Common.Cache
|
||||
{
|
||||
public interface ICacheWriter
|
||||
{
|
||||
//void AddCache(string key, object value, DateTime expDate);
|
||||
//void AddCache(string key, object value);
|
||||
//object GetCache(string key);
|
||||
//T GetCache<T>(string key);
|
||||
//void SetCache(string key, object value, DateTime expDate);
|
||||
//void SetCache(string key, object value);
|
||||
//bool Delete(string key);
|
||||
|
||||
string Get(string key);
|
||||
|
||||
string GetString(string key);
|
||||
|
||||
void AddString(string key, string value);
|
||||
|
||||
void Add(string key, string value, int ExpirationTime = 20);
|
||||
|
||||
void Remove(string key);
|
||||
|
||||
void Replace(string key, string value, int ExpirationTime = 20);
|
||||
}
|
||||
}
|
||||
96
CC.Yi.Common/Cache/RedisCacheService.cs
Normal file
96
CC.Yi.Common/Cache/RedisCacheService.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Caching.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CC.Yi.Common.Cache
|
||||
{
|
||||
public class RedisCacheService :ICacheWriter
|
||||
{
|
||||
private RedisCache _redisCache = null;
|
||||
public RedisCacheService(RedisCacheOptions options)
|
||||
{
|
||||
_redisCache = new RedisCache(options);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <returns></returns>
|
||||
public string Get(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrEmpty(key))
|
||||
{
|
||||
return Encoding.UTF8.GetString(_redisCache.Get(key));
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="value">缓存值</param>
|
||||
/// <param name="ExpirationTime">绝对过期时间(分钟)</param>
|
||||
public void Add(string key, string value, int ExpirationTime = 20)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(key))
|
||||
{
|
||||
_redisCache.Set(key, Encoding.UTF8.GetBytes(value), new DistributedCacheEntryOptions()
|
||||
{
|
||||
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(ExpirationTime)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void AddString(string key, string value)
|
||||
{
|
||||
_redisCache.SetString(key, value);
|
||||
}
|
||||
|
||||
public string GetString(string key)
|
||||
|
||||
{
|
||||
return _redisCache.GetString(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
public void Remove(string key)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(key))
|
||||
{
|
||||
_redisCache.Remove(key);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 更新缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="value">缓存值</param>
|
||||
/// <param name="ExpirationTime"></param>
|
||||
public void Replace(string key, string value, int ExpirationTime = 20)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(key))
|
||||
{
|
||||
_redisCache.Remove(key);
|
||||
_redisCache.Set(key, Encoding.UTF8.GetBytes(value), new DistributedCacheEntryOptions()
|
||||
{
|
||||
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(ExpirationTime)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace CC.Yi.Common
|
||||
{
|
||||
public static class JsonFactory
|
||||
public static class JsonHelper
|
||||
{
|
||||
public static string JsonToString(object q)
|
||||
{
|
||||
Reference in New Issue
Block a user