Skip to content

Commit

Permalink
调整项目结构,优化SqlServer的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
zgcwkj committed Jun 22, 2022
1 parent 8a984ee commit b0b089d
Show file tree
Hide file tree
Showing 26 changed files with 486 additions and 3,018 deletions.
4 changes: 2 additions & 2 deletions zgcwkj.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ static void Main(string[] args)

//Cache
Console.WriteLine("Cache >");
DataFactory.Cache.Set("zgcwkj", userID,0);
Console.WriteLine(DataFactory.Cache.Get<string>("zgcwkj"));
CacheAccess.Set("zgcwkj", userID,0);
Console.WriteLine(CacheAccess.Get<string>("zgcwkj"));

//DbContext
using var myDbContext = new MyDbContext();
Expand Down
6 changes: 0 additions & 6 deletions zgcwkj.Demo/zgcwkj.Demo.csproj.user

This file was deleted.

6 changes: 0 additions & 6 deletions zgcwkj.Model/zgcwkj.Model.csproj.user

This file was deleted.

22 changes: 1 addition & 21 deletions zgcwkj.Util/Data/Cache/CacheFactory.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System;
using zgcwkj.Util.Data.Cache.Memory;
using zgcwkj.Util.Data.Cache.Redis;
using zgcwkj.Util.Enum;
using zgcwkj.Util.Interface;

namespace zgcwkj.Util.Data
{
/// <summary>
/// 缓存工厂
/// </summary>
public class CacheFactory
internal class CacheFactory
{
/// <summary>
/// 缓存类型
Expand Down Expand Up @@ -45,22 +42,5 @@ public static string Connect
return dbConnect;
}
}

/// <summary>
/// 缓存对象
/// </summary>
public static ICache Cache
{
get
{
ICache cache = Type switch
{
CacheType.Memory => new MemoryImp(),
CacheType.Redis => new RedisImp(),
_ => throw new Exception("未找到缓存配置"),
};
return cache;
}
}
}
}
150 changes: 150 additions & 0 deletions zgcwkj.Util/Data/CacheAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System;
using zgcwkj.Util.Data;
using zgcwkj.Util.Data.Cache.Memory;
using zgcwkj.Util.Data.Cache.Redis;
using zgcwkj.Util.Enum;
using zgcwkj.Util.Interface;

namespace zgcwkj.Util
{
/// <summary>
/// <b>缓存操作对象</b>
///
/// <para>常规使用:var cache = CacheProvider.Create()</para>
/// <para>注入使用:services.AddTransient&lt;CacheAccess&gt;()</para>
/// </summary>
public class CacheAccess : IDisposable
{
/// <summary>
/// 缓存对象
/// </summary>
internal ICache Cache { get; set; }

/// <summary>
/// 实例时
/// </summary>
public CacheAccess()
{
Cache = CacheFactory.Type switch
{
CacheType.Memory => new MemoryImp(),
CacheType.Redis => new RedisImp(),
_ => throw new Exception("未找到缓存配置"),
};
}

/// <summary>
/// 释放对象
/// </summary>
public void Dispose()
{

}

/// <summary>
/// Key 是否存在
/// </summary>
/// <param name="key">键</param>
/// <param name="db">数据库索引</param>
/// <returns>存在</returns>
public static bool Exists(string key, int db = -1)
{
using var cacheAccess = new CacheAccess();
return cacheAccess.Exists(key, db);
}

/// <summary>
/// 设置缓存
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="db">数据库索引</param>
/// <param name="timeSpan">时间差</param>
/// <returns>状态</returns>
public static bool Set<T>(string key, T value, int db = -1, TimeSpan timeSpan = default)
{
using var cacheAccess = new CacheAccess();
return cacheAccess.Set(key, value, db);
}

/// <summary>
/// 获取缓存
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">键</param>
/// <param name="db">数据库索引</param>
/// <returns>数据</returns>
public static T Get<T>(string key, int db = -1)
{
using var cacheAccess = new CacheAccess();
return cacheAccess.Get<T>(key, db);
}

/// <summary>
/// 删除缓存
/// </summary>
/// <param name="key">类型</param>
/// <param name="db">数据库索引</param>
/// <returns>状态</returns>
public static bool Remove(string key, int db = -1)
{
using var cacheAccess = new CacheAccess();
return cacheAccess.Remove(key, db);
}

/// <summary>
/// Key 是否存在(哈希)
/// </summary>
/// <param name="key">键</param>
/// <param name="hashKey">哈希键</param>
/// <param name="db">数据库索引</param>
/// <returns>存在</returns>
public static bool HashExists(string key, string hashKey, int db = -1)
{
using var cacheAccess = new CacheAccess();
return cacheAccess.HashExists(key, hashKey, db);
}

/// <summary>
/// 设置缓存(哈希)
/// </summary>
/// <param name="key">键</param>
/// <param name="hashKey">哈希键</param>
/// <param name="hashValue">哈希值</param>
/// <param name="db">数据库索引</param>
/// <returns>状态</returns>
public static bool HashSet<T>(string key, string hashKey, T hashValue, int db = -1)
{
using var cacheAccess = new CacheAccess();
return cacheAccess.HashSet(key, hashKey, hashValue, db);
}

/// <summary>
/// 获取缓存(哈希)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">键</param>
/// <param name="hashKey">哈希键</param>
/// <param name="db">数据库索引</param>
/// <returns>数据</returns>
public static T HashGet<T>(string key, string hashKey, int db = -1)
{
using var cacheAccess = new CacheAccess();
return cacheAccess.HashGet<T>(key, hashKey, db);
}

/// <summary>
/// 删除缓存(哈希)
/// </summary>
/// <param name="key">键</param>
/// <param name="hashKey">哈希键</param>
/// <param name="db">数据库索引</param>
/// <returns>状态</returns>
public static long HashRemove(string key, string hashKey, int db = -1)
{
using var cacheAccess = new CacheAccess();
return cacheAccess.HashRemove(key, hashKey, db);
}
}
}
128 changes: 128 additions & 0 deletions zgcwkj.Util/Data/CacheProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;

namespace zgcwkj.Util
{
/// <summary>
/// <b>缓存操作提供者</b>
///
/// <para>常规使用:var cache = CacheProvider.Create()</para>
/// <para>注入使用:services.AddTransient&lt;CacheAccess&gt;()</para>
/// </summary>
public static class CacheProvider
{
/// <summary>
/// 创建缓存命令
/// </summary>
/// <returns></returns>
public static CacheAccess Create()
{
return new CacheAccess();
}

/// <summary>
/// Key 是否存在
/// </summary>
/// <param name="cacheAccess">对象</param>
/// <param name="key">键</param>
/// <param name="db">数据库索引</param>
/// <returns>存在</returns>
public static bool Exists(this CacheAccess cacheAccess, string key, int db = -1)
{
return cacheAccess.Exists(key, db);
}

/// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheAccess">对象</param>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="db">数据库索引</param>
/// <param name="timeSpan">时间差</param>
/// <returns>状态</returns>
public static bool Set<T>(this CacheAccess cacheAccess, string key, T value, int db = -1, TimeSpan timeSpan = default)
{
return cacheAccess.Set(key, value, db);
}

/// <summary>
/// 获取缓存
/// </summary>
/// <param name="cacheAccess">对象</param>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">键</param>
/// <param name="db">数据库索引</param>
/// <returns>数据</returns>
public static T Get<T>(this CacheAccess cacheAccess, string key, int db = -1)
{
return cacheAccess.Get<T>(key, db);
}

/// <summary>
/// 删除缓存
/// </summary>
/// <param name="cacheAccess">对象</param>
/// <param name="key">类型</param>
/// <param name="db">数据库索引</param>
/// <returns>状态</returns>
public static bool Remove(this CacheAccess cacheAccess, string key, int db = -1)
{
return cacheAccess.Remove(key, db);
}

/// <summary>
/// Key 是否存在(哈希)
/// </summary>
/// <param name="cacheAccess">对象</param>
/// <param name="key">键</param>
/// <param name="hashKey">哈希键</param>
/// <param name="db">数据库索引</param>
/// <returns>存在</returns>
public static bool HashExists(this CacheAccess cacheAccess, string key, string hashKey, int db = -1)
{
return cacheAccess.HashExists(key, hashKey, db);
}

/// <summary>
/// 设置缓存(哈希)
/// </summary>
/// <param name="cacheAccess">对象</param>
/// <param name="key">键</param>
/// <param name="hashKey">哈希键</param>
/// <param name="hashValue">哈希值</param>
/// <param name="db">数据库索引</param>
/// <returns>状态</returns>
public static bool HashSet<T>(this CacheAccess cacheAccess, string key, string hashKey, T hashValue, int db = -1)
{
return cacheAccess.HashSet(key, hashKey, hashValue, db);
}

/// <summary>
/// 获取缓存(哈希)
/// </summary>
/// <param name="cacheAccess">对象</param>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">键</param>
/// <param name="hashKey">哈希键</param>
/// <param name="db">数据库索引</param>
/// <returns>数据</returns>
public static T HashGet<T>(this CacheAccess cacheAccess, string key, string hashKey, int db = -1)
{
return cacheAccess.HashGet<T>(key, hashKey, db);
}

/// <summary>
/// 删除缓存(哈希)
/// </summary>
/// <param name="cacheAccess">对象</param>
/// <param name="key">键</param>
/// <param name="hashKey">哈希键</param>
/// <param name="db">数据库索引</param>
/// <returns>状态</returns>
public static long HashRemove(this CacheAccess cacheAccess, string key, string hashKey, int db = -1)
{
return cacheAccess.HashRemove(key, hashKey, db);
}
}
}
4 changes: 3 additions & 1 deletion zgcwkj.Util/Data/DataBase/DbCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ namespace zgcwkj.Util.Data
/// <summary>
/// <b>数据库连接对象</b>
///
/// <para>继承此对象可以实现原生操作</para>
/// <para>常规使用:using var dbComm = new DbContext()</para>
/// <para>注入使用:services.AddDbContext&lt;DbContext&gt;()</para>
/// <para>继承此对象可以实现原生操作!by zgcwkj</para>
/// </summary>
public class DbCommon : DbContext, IDisposable
{
Expand Down
Loading

0 comments on commit b0b089d

Please sign in to comment.