Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ligengrong committed May 28, 2024
2 parents 484a097 + 715a10c commit 85c88d7
Show file tree
Hide file tree
Showing 36 changed files with 333 additions and 7,912 deletions.
Binary file added DLL/MySql/MySql.Data_net80_v8.4.0_20240514.zip
Binary file not shown.
Binary file not shown.
Binary file added DLL/Oracle/Oracle.ManagedDataAccess_v23.4.zip
Binary file not shown.
7,612 changes: 0 additions & 7,612 deletions DLL/PostgreSQL/Npgsql.XML

This file was deleted.

Binary file removed DLL/PostgreSQL/Npgsql.dll
Binary file not shown.
Binary file added DLL/PostgreSQL/Npgsql_net80_v8.0.3_20240514.zip
Binary file not shown.
Binary file removed DLL/PostgreSQL/System.Threading.Tasks.Extensions.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<ItemGroup>
<PackageReference Include="IBM.Data.DB2.Core" Version="3.1.0.600" />
<PackageReference Include="MySql.Data" Version="8.4.0" />
<PackageReference Include="NewLife.Redis" Version="5.6.2024.402" />
<PackageReference Include="NewLife.Redis" Version="5.6.2024.508" />
<PackageReference Include="NewLife.Remoting" Version="2.8.2024.402" />
<PackageReference Include="NewLife.Stardust" Version="2.9.2024.402" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="23.4.0" />
Expand Down
188 changes: 92 additions & 96 deletions XCode/Cache/IEntityCache.cs
Original file line number Diff line number Diff line change
@@ -1,99 +1,95 @@
using System;
using System.Collections.Generic;
namespace XCode.Cache;

namespace XCode.Cache
/// <summary>缓存基接口</summary>
public interface IEntityCacheBase
{
/// <summary>缓存基接口</summary>
public interface IEntityCacheBase
{
/// <summary>连接名</summary>
String ConnName { get; set; }

/// <summary>表名</summary>
String TableName { get; set; }
}

/// <summary>实体缓存接口</summary>
public interface IEntityCache : IEntityCacheBase
{
/// <summary>实体集合。因为涉及一个转换,数据量大时很耗性能,建议不要使用。</summary>
IList<IEntity> Entities { get; }

/// <summary>清除缓存</summary>
/// <param name="reason">清除原因</param>
/// <param name="force">强制清除,下次访问阻塞等待。默认false仅置为过期,下次访问异步更新</param>
void Clear(String reason, Boolean force = false);
}

/// <summary>单对象缓存接口</summary>
public interface ISingleEntityCache : IEntityCacheBase
{
/// <summary>过期时间。单位是秒,默认60秒</summary>
Int32 Expire { get; set; }

/// <summary>最大实体数。默认10000</summary>
Int32 MaxEntity { get; set; }

/// <summary>是否在使用缓存</summary>
Boolean Using { get; set; }

/// <summary>获取数据</summary>
/// <param name="key"></param>
/// <returns></returns>
IEntity this[Object key] { get; }

/// <summary>根据从键获取实体数据</summary>
/// <param name="slaveKey"></param>
/// <returns></returns>
IEntity GetItemWithSlaveKey(String slaveKey);

/// <summary>是否包含指定主键</summary>
/// <param name="key"></param>
/// <returns></returns>
Boolean ContainsKey(Object key);

/// <summary>是否包含指定从键</summary>
/// <param name="key"></param>
/// <returns></returns>
Boolean ContainsSlaveKey(String key);

/// <summary>向单对象缓存添加项</summary>
/// <param name="value">实体对象</param>
/// <returns></returns>
Boolean Add(IEntity value);

/// <summary>移除指定项</summary>
/// <param name="entity"></param>
void Remove(IEntity entity);

/// <summary>清除所有数据</summary>
/// <param name="reason">清除缓存原因</param>
void Clear(String reason);
}

/// <summary></summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TEntity"></typeparam>
public interface ISingleEntityCache<TKey, TEntity> : ISingleEntityCache where TEntity : Entity<TEntity>, new()
{
/// <summary>获取数据</summary>
/// <param name="key"></param>
/// <returns></returns>
TEntity this[TKey key] { get; }

/// <summary>获取缓存主键的方法,默认方法为获取实体主键值</summary>
Func<TEntity, TKey> GetKeyMethod { get; set; }

/// <summary>查找数据的方法</summary>
Func<TKey, TEntity> FindKeyMethod { get; set; }

/// <summary>从键是否区分大小写</summary>
Boolean SlaveKeyIgnoreCase { get; set; }

/// <summary>根据从键查找数据的方法</summary>
Func<String, TEntity> FindSlaveKeyMethod { get; set; }

/// <summary>获取缓存从键的方法,默认为空</summary>
Func<TEntity, String> GetSlaveKeyMethod { get; set; }
}
/// <summary>连接名</summary>
String ConnName { get; set; }

/// <summary>表名</summary>
String TableName { get; set; }
}

/// <summary>实体缓存接口</summary>
public interface IEntityCache : IEntityCacheBase
{
/// <summary>实体集合。因为涉及一个转换,数据量大时很耗性能,建议不要使用。</summary>
IList<IEntity> Entities { get; }

/// <summary>清除缓存</summary>
/// <param name="reason">清除原因</param>
/// <param name="force">强制清除,下次访问阻塞等待。默认false仅置为过期,下次访问异步更新</param>
void Clear(String reason, Boolean force = false);
}

/// <summary>单对象缓存接口</summary>
public interface ISingleEntityCache : IEntityCacheBase
{
/// <summary>过期时间。单位是秒,默认60秒</summary>
Int32 Expire { get; set; }

/// <summary>最大实体数。默认10000</summary>
Int32 MaxEntity { get; set; }

/// <summary>是否在使用缓存</summary>
Boolean Using { get; set; }

/// <summary>获取数据</summary>
/// <param name="key"></param>
/// <returns></returns>
IEntity this[Object key] { get; }

/// <summary>根据从键获取实体数据</summary>
/// <param name="slaveKey"></param>
/// <returns></returns>
IEntity GetItemWithSlaveKey(String slaveKey);

/// <summary>是否包含指定主键</summary>
/// <param name="key"></param>
/// <returns></returns>
Boolean ContainsKey(Object key);

/// <summary>是否包含指定从键</summary>
/// <param name="key"></param>
/// <returns></returns>
Boolean ContainsSlaveKey(String key);

/// <summary>向单对象缓存添加项</summary>
/// <param name="value">实体对象</param>
/// <returns></returns>
Boolean Add(IEntity value);

/// <summary>移除指定项</summary>
/// <param name="entity"></param>
void Remove(IEntity entity);

/// <summary>清除所有数据</summary>
/// <param name="reason">清除缓存原因</param>
void Clear(String reason);
}

/// <summary></summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TEntity"></typeparam>
public interface ISingleEntityCache<TKey, TEntity> : ISingleEntityCache where TEntity : Entity<TEntity>, new()
{
/// <summary>获取数据</summary>
/// <param name="key"></param>
/// <returns></returns>
TEntity this[TKey key] { get; }

/// <summary>获取缓存主键的方法,默认方法为获取实体主键值</summary>
Func<TEntity, TKey> GetKeyMethod { get; set; }

/// <summary>查找数据的方法</summary>
Func<TKey, TEntity> FindKeyMethod { get; set; }

/// <summary>从键是否区分大小写</summary>
Boolean SlaveKeyIgnoreCase { get; set; }

/// <summary>根据从键查找数据的方法</summary>
Func<String, TEntity>? FindSlaveKeyMethod { get; set; }

/// <summary>获取缓存从键的方法,默认为空</summary>
Func<TEntity, String>? GetSlaveKeyMethod { get; set; }
}
34 changes: 14 additions & 20 deletions XCode/Cache/SingleEntityCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace XCode.Cache;
/// <typeparam name="TKey">键值类型</typeparam>
/// <typeparam name="TEntity">实体类型</typeparam>
public class SingleEntityCache<TKey, TEntity> : CacheBase<TEntity>, ISingleEntityCache<TKey, TEntity>
where TKey : notnull
where TEntity : Entity<TEntity>, new()
{
#region 属性
Expand All @@ -36,25 +37,25 @@ public class SingleEntityCache<TKey, TEntity> : CacheBase<TEntity>, ISingleEntit

#region 主键
/// <summary>获取缓存主键的方法,默认方法为获取实体主键值</summary>
public Func<TEntity, TKey> GetKeyMethod { get; set; }
public Func<TEntity, TKey> GetKeyMethod { get; set; } = null!;

/// <summary>查找数据的方法</summary>
public Func<TKey, TEntity> FindKeyMethod { get; set; }
public Func<TKey, TEntity> FindKeyMethod { get; set; } = null!;
#endregion

#region 从键
/// <summary>从键是否区分大小写</summary>
public Boolean SlaveKeyIgnoreCase { get; set; }

/// <summary>根据从键查找数据的方法</summary>
public Func<String, TEntity> FindSlaveKeyMethod { get; set; }
public Func<String, TEntity>? FindSlaveKeyMethod { get; set; }

/// <summary>获取缓存从键的方法,默认为空</summary>
public Func<TEntity, String> GetSlaveKeyMethod { get; set; }
public Func<TEntity, String>? GetSlaveKeyMethod { get; set; }
#endregion

#region 构造
/// <summary>实例化一个实体缓存</summary>
/// <summary>实例化一个单对象缓存</summary>
public SingleEntityCache()
{
var exp = XCodeSetting.Current.SingleCacheExpire;
Expand Down Expand Up @@ -90,7 +91,7 @@ protected override void Dispose(Boolean disposing)
#endregion

#region 检查过期缓存
private TimerX _Timer;
private TimerX? _Timer;
private void StartTimer()
{
if (_Timer == null)
Expand Down Expand Up @@ -167,26 +168,23 @@ private void CheckExpire(Object state)

#region 缓存对象
/// <summary>缓存对象</summary>
class CacheItem
class CacheItem(TKey key, String? slaveKey)
{
/// <summary>键</summary>
public TKey Key { get; set; }
public TKey Key { get; set; } = key;

/// <summary>从键</summary>
public String SlaveKey { get; set; }
public String? SlaveKey { get; set; } = slaveKey;

/// <summary>实体</summary>
public TEntity Entity { get; set; }
public TEntity? Entity { get; set; }

/// <summary>访问时间</summary>
public DateTime VisitTime { get; set; }

/// <summary>缓存过期时间</summary>
public DateTime ExpireTime { get; set; }

///// <summary>是否已经过期</summary>
//public Boolean Expired => ExpireTime <= TimerX.Now;

public void SetEntity(TEntity entity, Int32 expire)
{
Entity = entity;
Expand All @@ -207,7 +205,7 @@ public void SetEntity(TEntity entity, Int32 expire)
/// <summary>单对象缓存</summary>
private readonly ConcurrentDictionary<TKey, CacheItem> Entities = new();

private ConcurrentDictionary<String, CacheItem> _SlaveEntities;
private ConcurrentDictionary<String, CacheItem>? _SlaveEntities;
/// <summary>单对象缓存,从键查询使用</summary>
private ConcurrentDictionary<String, CacheItem> SlaveEntities
{
Expand Down Expand Up @@ -363,12 +361,8 @@ private CacheItem AddItem(TKey key, TEntity entity)

var skey = entity == null ? null : GetSlaveKeyMethod?.Invoke(entity);

var item = new CacheItem
{
Key = key,
SlaveKey = skey
};
item.SetEntity(entity, Expire);
var item = new CacheItem(key, skey);
item.SetEntity(entity!, Expire);

//var es = Entities;
//// 新增或更新
Expand Down
8 changes: 7 additions & 1 deletion XCode/DataAccessLayer/Common/DbBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,13 @@ public virtual SelectBuilder PageSplit(SelectBuilder builder, Int64 startRowInde
/// </remarks>
/// <param name="dateTime">时间值</param>
/// <returns></returns>
public virtual String FormatDateTime(DateTime dateTime) => "'" + dateTime.ToFullString() + "'";
public virtual String FormatDateTime(DateTime dateTime)
{
if (dateTime.Ticks % 10_000_000 == 0)
return $"'{dateTime:yyyy-MM-dd HH:mm:ss}'";
else
return $"'{dateTime:yyyy-MM-dd HH:mm:ss.fffffff}'";
}

/// <summary>格式化关键字</summary>
/// <param name="keyWord">表名</param>
Expand Down
2 changes: 1 addition & 1 deletion XCode/DataAccessLayer/Database/Access.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public override String FormatName(String name)
/// <summary>格式化时间为SQL字符串</summary>
/// <param name="dateTime">时间值</param>
/// <returns></returns>
public override String FormatDateTime(DateTime dateTime) => "#" + dateTime.ToFullString() + "#";
public override String FormatDateTime(DateTime dateTime) => $"#{dateTime:yyyy-MM-dd HH:mm:ss.fff}#";

/// <summary>格式化关键字</summary>
/// <param name="keyWord">关键字</param>
Expand Down
13 changes: 10 additions & 3 deletions XCode/DataAccessLayer/Database/DB2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Text;
Expand Down Expand Up @@ -141,9 +142,15 @@ public override SelectBuilder PageSplit(SelectBuilder builder, Int64 startRowInd
/// <returns></returns>
public override String FormatDateTime(DateTime dt)
{
if (dt.Hour == 0 && dt.Minute == 0 && dt.Second == 0) return $"To_Date('{dt:yyyy-MM-dd}', 'YYYY-MM-DD')";
if (dt.Ticks % 10_000_000 == 0)
{
if (dt.Hour == 0 && dt.Minute == 0 && dt.Second == 0)
return $"To_Date('{dt:yyyy-MM-dd}', 'YYYY-MM-DD')";
else
return $"To_Date('{dt:yyyy-MM-dd HH:mm:ss}', 'YYYY-MM-DD HH24:MI:SS')";
}

return $"To_Date('{dt:yyyy-MM-dd HH:mm:ss}', 'YYYY-MM-DD HH24:MI:SS')";
return $"To_Date('{dt:yyyy-MM-dd HH:mm:ss.fffffff}', 'YYYY-MM-DD HH24:MI:SS.FF')";
}

public override String FormatValue(IDataColumn field, Object? value)
Expand Down
10 changes: 8 additions & 2 deletions XCode/DataAccessLayer/Database/Oracle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,15 @@ public override SelectBuilder PageSplit(SelectBuilder builder, Int64 startRowInd
/// <returns></returns>
public override String FormatDateTime(DateTime dt)
{
if (dt.Hour == 0 && dt.Minute == 0 && dt.Second == 0) return $"To_Date('{dt:yyyy-MM-dd}', 'YYYY-MM-DD')";
if (dt.Ticks % 10_000_000 == 0)
{
if (dt.Hour == 0 && dt.Minute == 0 && dt.Second == 0)
return $"To_Date('{dt:yyyy-MM-dd}', 'YYYY-MM-DD')";
else
return $"To_Date('{dt:yyyy-MM-dd HH:mm:ss}', 'YYYY-MM-DD HH24:MI:SS')";
}

return $"To_Date('{dt:yyyy-MM-dd HH:mm:ss}', 'YYYY-MM-DD HH24:MI:SS')";
return $"To_Date('{dt:yyyy-MM-dd HH:mm:ss.fffffff}', 'YYYY-MM-DD HH24:MI:SS.FF')";
}

public override String FormatValue(IDataColumn field, Object? value)
Expand Down
10 changes: 8 additions & 2 deletions XCode/DataAccessLayer/Database/SqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,13 @@ public static String PageSplitMaxMin(String sql, Int64 startRowIndex, Int64 maxi
/// <summary>格式化时间为SQL字符串</summary>
/// <param name="dateTime">时间值</param>
/// <returns></returns>
public override String FormatDateTime(DateTime dateTime) => "{ts'" + dateTime.ToFullString() + "'}";
public override String FormatDateTime(DateTime dateTime)
{
if (dateTime.Ticks % 10_000_000 == 0)
return $"{{ts'{dateTime:yyyy-MM-dd HH:mm:ss}'}}";
else
return $"{{ts'{dateTime:yyyy-MM-dd HH:mm:ss.fffffff}'}}";
}

/// <summary>格式化名称,如果是关键字,则格式化后返回,否则原样返回</summary>
/// <param name="name">名称</param>
Expand Down Expand Up @@ -465,7 +471,7 @@ public override String FormatValue(IDataColumn field, Object? value)
return base.FormatValue(field, value);
}

private static readonly Char[] _likeKeys = new[] { '[', ']', '%', '_' };
private static readonly Char[] _likeKeys = ['[', ']', '%', '_'];
/// <summary>格式化模糊搜索的字符串。处理转义字符</summary>
/// <param name="column">字段</param>
/// <param name="format">格式化字符串</param>
Expand Down
Loading

0 comments on commit 85c88d7

Please sign in to comment.