Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mall.oytour.com
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄奎
mall.oytour.com
Commits
84fdcc16
Commit
84fdcc16
authored
Aug 05, 2020
by
黄奎
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增页面
parent
9ecc86f5
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
1582 additions
and
0 deletions
+1582
-0
AOPHelper.cs
Mall.AOP/AOPHelper.cs
+26
-0
BaseInterceptorAttribute.cs
Mall.AOP/BaseInterceptorAttribute.cs
+23
-0
LogAttribute.cs
Mall.AOP/CustomerAttribute/LogAttribute.cs
+23
-0
TransactionCallHandlerAttribute.cs
....AOP/CustomerAttribute/TransactionCallHandlerAttribute.cs
+80
-0
IOCInterceptor.cs
Mall.AOP/IOCInterceptor.cs
+51
-0
Mall.AOP.csproj
Mall.AOP/Mall.AOP.csproj
+11
-0
RedisConnectionHelp.cs
Mall.CacheManager/Base/RedisConnectionHelp.cs
+167
-0
RedisHelper.cs
Mall.CacheManager/Base/RedisHelper.cs
+1142
-0
BaseRepository.cs
Mall.Repository/Core/BaseRepository.cs
+59
-0
No files found.
Mall.AOP/AOPHelper.cs
0 → 100644
View file @
84fdcc16
using
Castle.DynamicProxy
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
Mall.AOP
{
/// <summary>
/// AOP帮助类
/// </summary>
public
class
AOPHelper
{
/// <summary>
/// 生成代理类对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public
static
T
CreateAOPObject
<
T
>()
where
T
:
class
{
ProxyGenerator
generator
=
new
ProxyGenerator
();
IOCInterceptor
interceptor
=
new
IOCInterceptor
();
T
t
=
generator
.
CreateClassProxy
<
T
>(
interceptor
);
return
t
;
}
}
}
Mall.AOP/BaseInterceptorAttribute.cs
0 → 100644
View file @
84fdcc16
using
Castle.DynamicProxy
;
using
Mall.AOP.CustomerAttribute
;
using
System
;
using
System.Collections.Generic
;
using
System.Reflection
;
using
System.Text
;
using
System.Transactions
;
using
System.Linq
;
namespace
Mall.AOP
{
/// <summary>
/// AOP属性基类
/// </summary>
public
abstract
class
BaseInterceptorAttribute
:
Attribute
{
/// <summary>
/// 具体执行的方法
/// </summary>
public
abstract
Action
Do
(
IInvocation
invocation
,
Action
action
);
}
}
Mall.AOP/CustomerAttribute/LogAttribute.cs
0 → 100644
View file @
84fdcc16
using
Castle.DynamicProxy
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
Mall.AOP.CustomerAttribute
{
/// <summary>
/// 日志属性
/// </summary>
public
class
LogAttribute
:
BaseInterceptorAttribute
{
public
override
Action
Do
(
IInvocation
invocation
,
Action
action
)
{
return
()
=>
{
Console
.
WriteLine
(
"LogAttribute1"
);
action
.
Invoke
();
Console
.
WriteLine
(
"LogAttribute2"
);
};
}
}
}
Mall.AOP/CustomerAttribute/TransactionCallHandlerAttribute.cs
0 → 100644
View file @
84fdcc16
using
Castle.DynamicProxy
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Transactions
;
namespace
Mall.AOP.CustomerAttribute
{
/// <summary>
/// 事务属性
/// </summary>
[
AttributeUsage
(
AttributeTargets
.
Method
,
Inherited
=
true
)]
public
class
TransactionCallHandlerAttribute
:
BaseInterceptorAttribute
{
/// <summary>
/// 超时时间
/// </summary>
public
int
Timeout
{
get
;
set
;
}
/// <summary>
/// 事务范围
/// </summary>
public
TransactionScopeOption
ScopeOption
{
get
;
set
;
}
/// <summary>
/// 事务隔离级别
/// </summary>
public
IsolationLevel
IsolationLevel
{
get
;
set
;
}
/// <summary>
/// 构造函数
/// </summary>
public
TransactionCallHandlerAttribute
()
{
Timeout
=
60
;
ScopeOption
=
TransactionScopeOption
.
Required
;
IsolationLevel
=
IsolationLevel
.
ReadCommitted
;
}
/// <summary>
/// 执行方法
/// </summary>
/// <param name="invocation"></param>
/// <param name="action"></param>
/// <returns></returns>
public
override
Action
Do
(
IInvocation
invocation
,
Action
action
)
{
return
()
=>
{
Console
.
WriteLine
(
"TransactionCallHandlerAttribute1"
);
TransactionOptions
transactionOptions
=
new
TransactionOptions
();
//设置事务隔离级别
transactionOptions
.
IsolationLevel
=
this
.
IsolationLevel
;
//设置事务超时时间为60秒
transactionOptions
.
Timeout
=
new
TimeSpan
(
0
,
0
,
this
.
Timeout
);
using
(
TransactionScope
scope
=
new
TransactionScope
(
this
.
ScopeOption
,
transactionOptions
))
{
try
{
//实现事务性工作
action
.
Invoke
();
var
result
=
invocation
.
ReturnValue
;
bool
.
TryParse
((
result
!=
null
?
result
.
ToString
()
:
""
),
out
bool
IsSuccess
);
if
(
IsSuccess
)
{
scope
.
Complete
();
}
}
catch
(
Exception
ex
)
{
// 记录异常
throw
ex
;
}
}
Console
.
WriteLine
(
"TransactionCallHandlerAttribute2"
);
};
}
}
}
Mall.AOP/IOCInterceptor.cs
0 → 100644
View file @
84fdcc16
using
System
;
using
System.Collections.Generic
;
namespace
Mall.AOP
{
/// <summary>
/// 拦截器基类
/// </summary>
public
class
IOCInterceptor
:
Castle
.
DynamicProxy
.
StandardInterceptor
{
/// <summary>
/// 方法调用前执行
/// </summary>
/// <param name="invocation"></param>
protected
override
void
PreProceed
(
Castle
.
DynamicProxy
.
IInvocation
invocation
)
{
Console
.
WriteLine
(
"调用前的拦截器, 方法名是: {0}"
,
invocation
.
Method
.
Name
);
}
/// <summary>
/// 方法执行时调用
/// </summary>
/// <param name="invocation"></param>
protected
override
void
PerformProceed
(
Castle
.
DynamicProxy
.
IInvocation
invocation
)
{
var
method
=
invocation
.
Method
;
Action
action
=
()
=>
base
.
PerformProceed
(
invocation
);
if
(
method
.
IsDefined
(
typeof
(
BaseInterceptorAttribute
),
true
))
{
foreach
(
var
attribute
in
method
.
GetCustomAttributes
(
typeof
(
BaseInterceptorAttribute
),
true
))
{
var
attr
=
(
BaseInterceptorAttribute
)
attribute
;
action
=
attr
.
Do
(
invocation
,
action
);
}
}
action
.
Invoke
();
Console
.
WriteLine
(
"拦截的方法返回时调用的拦截, 方法名是: {0}"
,
invocation
.
Method
.
Name
);
}
/// <summary>
/// 方法执行后调用
/// </summary>
/// <param name="invocation"></param>
protected
override
void
PostProceed
(
Castle
.
DynamicProxy
.
IInvocation
invocation
)
{
Console
.
WriteLine
(
"调用后的拦截器, 方法名是: {0}"
,
invocation
.
Method
.
Name
);
}
}
}
Mall.AOP/Mall.AOP.csproj
0 → 100644
View file @
84fdcc16
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.4.1" />
</ItemGroup>
</Project>
Mall.CacheManager/Base/RedisConnectionHelp.cs
0 → 100644
View file @
84fdcc16
using
Microsoft.Extensions.Configuration
;
using
Microsoft.Extensions.Configuration.Json
;
using
StackExchange.Redis
;
using
System
;
using
System.Collections.Concurrent
;
using
System.Collections.Generic
;
using
System.Configuration
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Mall.CacheManager.Base
{
/// <summary>
/// ConnectionMultiplexer对象管理帮助类
/// </summary>
public
static
class
RedisConnectionHelp
{
/// <summary>
/// 系统自定义Key前缀
/// </summary>
public
static
readonly
string
SysCustomKey
=
""
;
/// <summary>
/// 连接字符串
/// </summary>
private
static
readonly
string
RedisConnectionString
=
$@"
{
new
ConfigurationBuilder
().
Add
(
new
JsonConfigurationSource
{
Path
=
"appsettings.json"
}).
Build
().
GetSection
(
"RedisSetting"
)[
"RedisServer"
]}
:
{
new
ConfigurationBuilder
().
Add
(
new
JsonConfigurationSource
{
Path
=
"appsettings.json"
}).
Build
().
GetSection
(
"RedisSetting"
)[
"RedisPort"
]}
,allowadmin=true,password=
{
new
ConfigurationBuilder
().
Add
(
new
JsonConfigurationSource
{
Path
=
"appsettings.json"
}).
Build
().
GetSection
(
"RedisSetting"
)[
"RedisPwd"
]}
"
;
private
static
readonly
object
Locker
=
new
object
();
private
static
ConnectionMultiplexer
_instance
;
/// <summary>
/// 线程安全的字典
/// </summary>
private
static
readonly
ConcurrentDictionary
<
string
,
ConnectionMultiplexer
>
ConnectionCache
=
new
ConcurrentDictionary
<
string
,
ConnectionMultiplexer
>();
/// <summary>
/// 单例获取
/// </summary>
public
static
ConnectionMultiplexer
Instance
{
get
{
if
(
_instance
==
null
)
{
lock
(
Locker
)
{
if
(
_instance
==
null
||
!
_instance
.
IsConnected
)
{
_instance
=
GetManager
();
}
}
}
return
_instance
;
}
}
/// <summary>
/// 缓存获取
/// </summary>
/// <param name="connectionString"></param>
/// <returns></returns>
public
static
ConnectionMultiplexer
GetConnectionMultiplexer
(
string
connectionString
)
{
if
(!
ConnectionCache
.
ContainsKey
(
connectionString
))
{
ConnectionCache
[
connectionString
]
=
GetManager
(
connectionString
);
}
return
ConnectionCache
[
connectionString
];
}
/// <summary>
/// 获取连接
/// </summary>
/// <param name="connectionString"></param>
/// <returns></returns>
private
static
ConnectionMultiplexer
GetManager
(
string
connectionString
=
null
)
{
connectionString
=
connectionString
??
RedisConnectionString
;
var
connect
=
ConnectionMultiplexer
.
Connect
(
connectionString
);
//注册如下事件
connect
.
ConnectionFailed
+=
MuxerConnectionFailed
;
connect
.
ConnectionRestored
+=
MuxerConnectionRestored
;
connect
.
ErrorMessage
+=
MuxerErrorMessage
;
connect
.
ConfigurationChanged
+=
MuxerConfigurationChanged
;
connect
.
HashSlotMoved
+=
MuxerHashSlotMoved
;
connect
.
InternalError
+=
MuxerInternalError
;
connect
.
ConfigurationChangedBroadcast
+=
MuxerConfigurationChangedBroadcast
;
return
connect
;
}
#
region
事件
/// <summary>
/// 重新配置广播时(通常意味着主从同步更改)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
static
void
MuxerConfigurationChangedBroadcast
(
object
sender
,
EndPointEventArgs
e
)
{
Console
.
WriteLine
(
$"
{
nameof
(
MuxerConfigurationChangedBroadcast
)}
:
{
e
.
EndPoint
}
"
);
}
/// <summary>
/// 配置更改时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
static
void
MuxerConfigurationChanged
(
object
sender
,
EndPointEventArgs
e
)
{
Console
.
WriteLine
(
"Configuration changed: "
+
e
.
EndPoint
);
}
/// <summary>
/// 发生内部错误时(主要用于调试)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
static
void
MuxerErrorMessage
(
object
sender
,
RedisErrorEventArgs
e
)
{
Console
.
WriteLine
(
"ErrorMessage: "
+
e
.
Message
);
}
/// <summary>
/// 重新建立连接之前的错误
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
static
void
MuxerConnectionRestored
(
object
sender
,
ConnectionFailedEventArgs
e
)
{
Console
.
WriteLine
(
"ConnectionRestored: "
+
e
.
EndPoint
);
}
/// <summary>
/// 连接失败 , 如果重新连接成功你将不会收到这个通知
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
static
void
MuxerConnectionFailed
(
object
sender
,
ConnectionFailedEventArgs
e
)
{
Console
.
WriteLine
(
"重新连接:Endpoint failed: "
+
e
.
EndPoint
+
", "
+
e
.
FailureType
+
(
e
.
Exception
==
null
?
""
:
(
", "
+
e
.
Exception
.
Message
)));
}
/// <summary>
/// 更改集群时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
static
void
MuxerHashSlotMoved
(
object
sender
,
HashSlotMovedEventArgs
e
)
{
Console
.
WriteLine
(
"HashSlotMoved:NewEndPoint"
+
e
.
NewEndPoint
+
", OldEndPoint"
+
e
.
OldEndPoint
);
}
/// <summary>
/// redis类库错误
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
static
void
MuxerInternalError
(
object
sender
,
InternalErrorEventArgs
e
)
{
Console
.
WriteLine
(
"InternalError:Message"
+
e
.
Exception
.
Message
);
}
#
endregion
}
}
Mall.CacheManager/Base/RedisHelper.cs
0 → 100644
View file @
84fdcc16
using
Newtonsoft.Json
;
using
StackExchange.Redis
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Mall.CacheManager.Base
{
/// <summary>
/// Redis操作
/// </summary>
public
class
RedisHelper
{
private
readonly
ConnectionMultiplexer
_conn
;
/// <summary>
/// 用户自定义前缀
/// </summary>
public
string
CustomKey
;
private
int
DbNum
{
get
;
set
;
}
#
region
构造函数
/// <summary>
/// 构造函数
/// </summary>
/// <param name="dbNum"></param>
/// <param name="customKey">用户自定义前缀</param>
public
RedisHelper
(
int
dbNum
=
0
,
string
customKey
=
null
)
:
this
(
dbNum
,
null
,
customKey
)
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="dbNum"></param>
/// <param name="readWriteHosts"></param>
/// <param name="customKey">用户自定义前缀</param>
public
RedisHelper
(
int
dbNum
,
string
readWriteHosts
,
string
customKey
)
{
DbNum
=
dbNum
;
CustomKey
=
customKey
;
_conn
=
string
.
IsNullOrWhiteSpace
(
readWriteHosts
)
?
RedisConnectionHelp
.
Instance
:
RedisConnectionHelp
.
GetConnectionMultiplexer
(
readWriteHosts
);
}
#
endregion
构造函数
#
region
String
#
region
同步方法
/// <summary>
/// 保存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public
async
void
Set
(
string
key
,
string
value
)
{
await
Do
(
db
=>
db
.
StringSetAsync
(
key
,
value
));
}
/// <summary>
/// 获取
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
string
Get
(
string
key
)
{
return
Do
(
db
=>
db
.
StringGet
(
key
));
}
/// <summary>
/// 保存单个key value
/// </summary>
/// <param name="key">Redis Key</param>
/// <param name="value">保存的值</param>
/// <param name="expiry">过期时间</param>
/// <returns></returns>
public
bool
StringSet
(
string
key
,
string
value
,
TimeSpan
?
expiry
=
default
(
TimeSpan
?))
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
StringSet
(
key
,
value
,
expiry
));
}
/// <summary>
/// 保存多个key value
/// </summary>
/// <param name="keyValues">键值对</param>
/// <returns></returns>
public
bool
StringSet
(
List
<
KeyValuePair
<
RedisKey
,
RedisValue
>>
keyValues
)
{
List
<
KeyValuePair
<
RedisKey
,
RedisValue
>>
newkeyValues
=
keyValues
.
Select
(
p
=>
new
KeyValuePair
<
RedisKey
,
RedisValue
>(
AddSysCustomKey
(
p
.
Key
),
p
.
Value
)).
ToList
();
return
Do
(
db
=>
db
.
StringSet
(
newkeyValues
.
ToArray
()));
}
/// <summary>
/// 保存一个对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="expiry"></param>
/// <returns></returns>
public
bool
StringSet
<
T
>(
string
key
,
T
obj
,
TimeSpan
?
expiry
=
default
(
TimeSpan
?))
{
key
=
AddSysCustomKey
(
key
);
string
json
=
ConvertJson
(
obj
);
return
Do
(
db
=>
db
.
StringSet
(
key
,
json
,
expiry
));
}
/// <summary>
/// 获取单个key的值
/// </summary>
/// <param name="key">Redis Key</param>
/// <returns></returns>
public
string
StringGet
(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
StringGet
(
key
));
}
/// <summary>
/// 获取多个Key
/// </summary>
/// <param name="listKey">Redis Key集合</param>
/// <returns></returns>
public
RedisValue
[]
StringGet
(
List
<
string
>
listKey
)
{
List
<
string
>
newKeys
=
listKey
.
Select
(
AddSysCustomKey
).
ToList
();
return
Do
(
db
=>
db
.
StringGet
(
ConvertRedisKeys
(
newKeys
)));
}
/// <summary>
/// 获取一个key的对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public
T
StringGet
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
ConvertObj
<
T
>(
db
.
StringGet
(
key
)));
}
/// <summary>
/// 为数字增长val
/// </summary>
/// <param name="key"></param>
/// <param name="val">可以为负</param>
/// <returns>增长后的值</returns>
public
long
StringIncrement
(
string
key
,
long
val
=
1
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
StringIncrement
(
key
,
val
));
}
/// <summary>
/// 为数字减少val
/// </summary>
/// <param name="key"></param>
/// <param name="val">可以为负</param>
/// <returns>减少后的值</returns>
public
long
StringDecrement
(
string
key
,
long
val
=
1
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
StringDecrement
(
key
,
val
));
}
#
endregion
同步方法
#
region
异步方法
/// <summary>
/// 保存单个key value
/// </summary>
/// <param name="key">Redis Key</param>
/// <param name="value">保存的值</param>
/// <param name="expiry">过期时间</param>
/// <returns></returns>
public
async
Task
<
bool
>
StringSetAsync
(
string
key
,
string
value
,
TimeSpan
?
expiry
=
default
(
TimeSpan
?))
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
StringSetAsync
(
key
,
value
,
expiry
));
}
/// <summary>
/// 保存多个key value
/// </summary>
/// <param name="keyValues">键值对</param>
/// <returns></returns>
public
async
Task
<
bool
>
StringSetAsync
(
List
<
KeyValuePair
<
RedisKey
,
RedisValue
>>
keyValues
)
{
List
<
KeyValuePair
<
RedisKey
,
RedisValue
>>
newkeyValues
=
keyValues
.
Select
(
p
=>
new
KeyValuePair
<
RedisKey
,
RedisValue
>(
AddSysCustomKey
(
p
.
Key
),
p
.
Value
)).
ToList
();
return
await
Do
(
db
=>
db
.
StringSetAsync
(
newkeyValues
.
ToArray
()));
}
/// <summary>
/// 保存一个对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="expiry"></param>
/// <returns></returns>
public
async
Task
<
bool
>
StringSetAsync
<
T
>(
string
key
,
T
obj
,
TimeSpan
?
expiry
=
default
(
TimeSpan
?))
{
key
=
AddSysCustomKey
(
key
);
string
json
=
ConvertJson
(
obj
);
return
await
Do
(
db
=>
db
.
StringSetAsync
(
key
,
json
,
expiry
));
}
/// <summary>
/// 获取单个key的值
/// </summary>
/// <param name="key">Redis Key</param>
/// <returns></returns>
public
async
Task
<
string
>
StringGetAsync
(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
StringGetAsync
(
key
));
}
/// <summary>
/// 获取多个Key
/// </summary>
/// <param name="listKey">Redis Key集合</param>
/// <returns></returns>
public
async
Task
<
RedisValue
[
]>
StringGetAsync
(
List
<
string
>
listKey
)
{
List
<
string
>
newKeys
=
listKey
.
Select
(
AddSysCustomKey
).
ToList
();
return
await
Do
(
db
=>
db
.
StringGetAsync
(
ConvertRedisKeys
(
newKeys
)));
}
/// <summary>
/// 获取一个key的对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public
async
Task
<
T
>
StringGetAsync
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
string
result
=
await
Do
(
db
=>
db
.
StringGetAsync
(
key
));
return
ConvertObj
<
T
>(
result
);
}
/// <summary>
/// 为数字增长val
/// </summary>
/// <param name="key"></param>
/// <param name="val">可以为负</param>
/// <returns>增长后的值</returns>
public
async
Task
<
double
>
StringIncrementAsync
(
string
key
,
double
val
=
1
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
StringIncrementAsync
(
key
,
val
));
}
/// <summary>
/// 为数字减少val
/// </summary>
/// <param name="key"></param>
/// <param name="val">可以为负</param>
/// <returns>减少后的值</returns>
public
async
Task
<
double
>
StringDecrementAsync
(
string
key
,
double
val
=
1
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
StringDecrementAsync
(
key
,
val
));
}
#
endregion
异步方法
#
endregion
String
#
region
Hash
#
region
同步方法
/// <summary>
/// 判断某个数据是否已经被缓存
/// </summary>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public
bool
HashExists
(
string
key
,
string
dataKey
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
HashExists
(
key
,
dataKey
));
}
/// <summary>
/// 存储数据到hash表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <param name="t"></param>
/// <returns></returns>
public
bool
HashSet
<
T
>(
string
key
,
string
dataKey
,
T
t
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
{
string
json
=
ConvertJson
(
t
);
return
db
.
HashSet
(
key
,
dataKey
,
json
);
});
}
/// <summary>
/// 移除hash中的某值
/// </summary>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public
bool
HashDelete
(
string
key
,
string
dataKey
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
HashDelete
(
key
,
dataKey
));
}
/// <summary>
/// 移除hash中的多个值
/// </summary>
/// <param name="key"></param>
/// <param name="dataKeys"></param>
/// <returns></returns>
public
long
HashDelete
(
string
key
,
List
<
RedisValue
>
dataKeys
)
{
key
=
AddSysCustomKey
(
key
);
//List<RedisValue> dataKeys1 = new List<RedisValue>() {"1","2"};
return
Do
(
db
=>
db
.
HashDelete
(
key
,
dataKeys
.
ToArray
()));
}
/// <summary>
/// 从hash表获取数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public
T
HashGet
<
T
>(
string
key
,
string
dataKey
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
{
string
value
=
db
.
HashGet
(
key
,
dataKey
);
return
ConvertObj
<
T
>(
value
);
});
}
/// <summary>
/// 为数字增长val
/// </summary>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <param name="val">可以为负</param>
/// <returns>增长后的值</returns>
public
double
HashIncrement
(
string
key
,
string
dataKey
,
double
val
=
1
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
HashIncrement
(
key
,
dataKey
,
val
));
}
/// <summary>
/// 为数字减少val
/// </summary>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <param name="val">可以为负</param>
/// <returns>减少后的值</returns>
public
double
HashDecrement
(
string
key
,
string
dataKey
,
double
val
=
1
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
HashDecrement
(
key
,
dataKey
,
val
));
}
/// <summary>
/// 获取hashkey所有Redis key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public
List
<
T
>
HashKeys
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
{
RedisValue
[]
values
=
db
.
HashKeys
(
key
);
return
ConvetList
<
T
>(
values
);
});
}
#
endregion
同步方法
#
region
异步方法
/// <summary>
/// 判断某个数据是否已经被缓存
/// </summary>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public
async
Task
<
bool
>
HashExistsAsync
(
string
key
,
string
dataKey
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
HashExistsAsync
(
key
,
dataKey
));
}
/// <summary>
/// 存储数据到hash表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <param name="t"></param>
/// <returns></returns>
public
async
Task
<
bool
>
HashSetAsync
<
T
>(
string
key
,
string
dataKey
,
T
t
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
{
string
json
=
ConvertJson
(
t
);
return
db
.
HashSetAsync
(
key
,
dataKey
,
json
);
});
}
/// <summary>
/// 移除hash中的某值
/// </summary>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public
async
Task
<
bool
>
HashDeleteAsync
(
string
key
,
string
dataKey
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
HashDeleteAsync
(
key
,
dataKey
));
}
/// <summary>
/// 移除hash中的多个值
/// </summary>
/// <param name="key"></param>
/// <param name="dataKeys"></param>
/// <returns></returns>
public
async
Task
<
long
>
HashDeleteAsync
(
string
key
,
List
<
RedisValue
>
dataKeys
)
{
key
=
AddSysCustomKey
(
key
);
//List<RedisValue> dataKeys1 = new List<RedisValue>() {"1","2"};
return
await
Do
(
db
=>
db
.
HashDeleteAsync
(
key
,
dataKeys
.
ToArray
()));
}
/// <summary>
/// 从hash表获取数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public
async
Task
<
T
>
HashGeAsync
<
T
>(
string
key
,
string
dataKey
)
{
key
=
AddSysCustomKey
(
key
);
string
value
=
await
Do
(
db
=>
db
.
HashGetAsync
(
key
,
dataKey
));
return
ConvertObj
<
T
>(
value
);
}
/// <summary>
/// 为数字增长val
/// </summary>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <param name="val">可以为负</param>
/// <returns>增长后的值</returns>
public
async
Task
<
double
>
HashIncrementAsync
(
string
key
,
string
dataKey
,
double
val
=
1
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
HashIncrementAsync
(
key
,
dataKey
,
val
));
}
/// <summary>
/// 为数字减少val
/// </summary>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <param name="val">可以为负</param>
/// <returns>减少后的值</returns>
public
async
Task
<
double
>
HashDecrementAsync
(
string
key
,
string
dataKey
,
double
val
=
1
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
HashDecrementAsync
(
key
,
dataKey
,
val
));
}
/// <summary>
/// 获取hashkey所有Redis key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public
async
Task
<
List
<
T
>>
HashKeysAsync
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
RedisValue
[]
values
=
await
Do
(
db
=>
db
.
HashKeysAsync
(
key
));
return
ConvetList
<
T
>(
values
);
}
#
endregion
异步方法
#
endregion
Hash
#
region
List
#
region
同步方法
/// <summary>
/// 移除指定ListId的内部List的值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public
void
ListRemove
<
T
>(
string
key
,
T
value
)
{
key
=
AddSysCustomKey
(
key
);
Do
(
db
=>
db
.
ListRemove
(
key
,
ConvertJson
(
value
)));
}
/// <summary>
/// 获取指定key的List
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
List
<
T
>
ListRange
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
redis
=>
{
var
values
=
redis
.
ListRange
(
key
);
return
ConvetList
<
T
>(
values
);
});
}
/// <summary>
/// 入队
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public
void
ListRightPush
<
T
>(
string
key
,
T
value
)
{
key
=
AddSysCustomKey
(
key
);
Do
(
db
=>
db
.
ListRightPush
(
key
,
ConvertJson
(
value
)));
}
/// <summary>
/// 出队
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public
T
ListRightPop
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
{
var
value
=
db
.
ListRightPop
(
key
);
return
ConvertObj
<
T
>(
value
);
});
}
/// <summary>
/// 入栈
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
public
void
ListLeftPush
<
T
>(
string
key
,
T
value
)
{
key
=
AddSysCustomKey
(
key
);
Do
(
db
=>
db
.
ListLeftPush
(
key
,
ConvertJson
(
value
)));
}
/// <summary>
/// 出栈
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public
T
ListLeftPop
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
{
var
value
=
db
.
ListLeftPop
(
key
);
return
ConvertObj
<
T
>(
value
);
});
}
/// <summary>
/// 获取集合中的数量
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
long
ListLength
(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
redis
=>
redis
.
ListLength
(
key
));
}
#
endregion
同步方法
#
region
异步方法
/// <summary>
/// 移除指定ListId的内部List的值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public
async
Task
<
long
>
ListRemoveAsync
<
T
>(
string
key
,
T
value
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
ListRemoveAsync
(
key
,
ConvertJson
(
value
)));
}
/// <summary>
/// 获取指定key的List
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
async
Task
<
List
<
T
>>
ListRangeAsync
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
var
values
=
await
Do
(
redis
=>
redis
.
ListRangeAsync
(
key
));
return
ConvetList
<
T
>(
values
);
}
/// <summary>
/// 入队
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public
async
Task
<
long
>
ListRightPushAsync
<
T
>(
string
key
,
T
value
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
ListRightPushAsync
(
key
,
ConvertJson
(
value
)));
}
/// <summary>
/// 出队
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public
async
Task
<
T
>
ListRightPopAsync
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
var
value
=
await
Do
(
db
=>
db
.
ListRightPopAsync
(
key
));
return
ConvertObj
<
T
>(
value
);
}
/// <summary>
/// 入栈
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
public
async
Task
<
long
>
ListLeftPushAsync
<
T
>(
string
key
,
T
value
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
ListLeftPushAsync
(
key
,
ConvertJson
(
value
)));
}
/// <summary>
/// 出栈
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public
async
Task
<
T
>
ListLeftPopAsync
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
var
value
=
await
Do
(
db
=>
db
.
ListLeftPopAsync
(
key
));
return
ConvertObj
<
T
>(
value
);
}
/// <summary>
/// 获取集合中的数量
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
async
Task
<
long
>
ListLengthAsync
(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
redis
=>
redis
.
ListLengthAsync
(
key
));
}
#
endregion
异步方法
#
endregion
List
#
region
SortedSet
有序集合
#
region
同步方法
/// <summary>
/// 添加
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="score"></param>
public
bool
SortedSetAdd
<
T
>(
string
key
,
T
value
,
double
score
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
redis
=>
redis
.
SortedSetAdd
(
key
,
ConvertJson
<
T
>(
value
),
score
));
}
/// <summary>
/// 删除
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public
bool
SortedSetRemove
<
T
>(
string
key
,
T
value
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
redis
=>
redis
.
SortedSetRemove
(
key
,
ConvertJson
(
value
)));
}
/// <summary>
/// 获取全部
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
List
<
T
>
SortedSetRangeByRank
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
redis
=>
{
var
values
=
redis
.
SortedSetRangeByRank
(
key
);
return
ConvetList
<
T
>(
values
);
});
}
/// <summary>
/// 获取集合中的数量
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
long
SortedSetLength
(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
redis
=>
redis
.
SortedSetLength
(
key
));
}
#
endregion
同步方法
#
region
异步方法
/// <summary>
/// 添加
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="score"></param>
public
async
Task
<
bool
>
SortedSetAddAsync
<
T
>(
string
key
,
T
value
,
double
score
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
redis
=>
redis
.
SortedSetAddAsync
(
key
,
ConvertJson
<
T
>(
value
),
score
));
}
/// <summary>
/// 删除
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public
async
Task
<
bool
>
SortedSetRemoveAsync
<
T
>(
string
key
,
T
value
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
redis
=>
redis
.
SortedSetRemoveAsync
(
key
,
ConvertJson
(
value
)));
}
/// <summary>
/// 获取全部
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
async
Task
<
List
<
T
>>
SortedSetRangeByRankAsync
<
T
>(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
var
values
=
await
Do
(
redis
=>
redis
.
SortedSetRangeByRankAsync
(
key
));
return
ConvetList
<
T
>(
values
);
}
/// <summary>
/// 获取集合中的数量
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
async
Task
<
long
>
SortedSetLengthAsync
(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
redis
=>
redis
.
SortedSetLengthAsync
(
key
));
}
#
endregion
异步方法
#
endregion
SortedSet
有序集合
#
region
key
#
region
同步
/// <summary>
/// 删除单个key
/// </summary>
/// <param name="key">redis key</param>
/// <returns>是否删除成功</returns>
public
bool
KeyDelete
(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
KeyDelete
(
key
));
}
/// <summary>
/// 删除多个key
/// </summary>
/// <param name="keys">rediskey</param>
/// <returns>成功删除的个数</returns>
public
long
KeyDelete
(
List
<
string
>
keys
)
{
List
<
string
>
newKeys
=
keys
.
Select
(
AddSysCustomKey
).
ToList
();
return
Do
(
db
=>
db
.
KeyDelete
(
ConvertRedisKeys
(
newKeys
)));
}
/// <summary>
/// 判断key是否存储
/// </summary>
/// <param name="key">redis key</param>
/// <returns></returns>
public
bool
KeyExists
(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
KeyExists
(
key
));
}
/// <summary>
/// 重新命名key
/// </summary>
/// <param name="key">就的redis key</param>
/// <param name="newKey">新的redis key</param>
/// <returns></returns>
public
bool
KeyRename
(
string
key
,
string
newKey
)
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
KeyRename
(
key
,
newKey
));
}
/// <summary>
/// 设置Key的时间
/// </summary>
/// <param name="key">redis key</param>
/// <param name="expiry"></param>
/// <returns></returns>
public
bool
KeyExpire
(
string
key
,
TimeSpan
?
expiry
=
default
(
TimeSpan
?))
{
key
=
AddSysCustomKey
(
key
);
return
Do
(
db
=>
db
.
KeyExpire
(
key
,
expiry
));
}
#
endregion
#
region
异步
/// <summary>
/// 删除单个key
/// </summary>
/// <param name="key">redis key</param>
/// <returns>是否删除成功</returns>
public
async
Task
<
bool
>
KeyDeleteAsync
(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
KeyDeleteAsync
(
key
));
}
/// <summary>
/// 删除多个key
/// </summary>
/// <param name="keys">rediskey</param>
/// <returns>成功删除的个数</returns>
public
async
Task
<
long
>
KeyDeleteAsync
(
List
<
string
>
keys
)
{
List
<
string
>
newKeys
=
keys
.
Select
(
AddSysCustomKey
).
ToList
();
return
await
Do
(
db
=>
db
.
KeyDeleteAsync
(
ConvertRedisKeys
(
newKeys
)));
}
/// <summary>
/// 判断key是否存储
/// </summary>
/// <param name="key">redis key</param>
/// <returns></returns>
public
async
Task
<
bool
>
KeyExistsAsync
(
string
key
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
KeyExistsAsync
(
key
));
}
/// <summary>
/// 重新命名key
/// </summary>
/// <param name="key">就的redis key</param>
/// <param name="newKey">新的redis key</param>
/// <returns></returns>
public
async
Task
<
bool
>
KeyRenameAsync
(
string
key
,
string
newKey
)
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
KeyRenameAsync
(
key
,
newKey
));
}
/// <summary>
/// 设置Key的时间
/// </summary>
/// <param name="key">redis key</param>
/// <param name="expiry"></param>
/// <returns></returns>
public
async
Task
<
bool
>
KeyExpireAsync
(
string
key
,
TimeSpan
?
expiry
=
default
(
TimeSpan
?))
{
key
=
AddSysCustomKey
(
key
);
return
await
Do
(
db
=>
db
.
KeyExpireAsync
(
key
,
expiry
));
}
#
endregion
#
endregion
key
#
region
发布订阅
#
region
同步
/// <summary>
/// 订阅
/// </summary>
/// <param name="subChannel"></param>
/// <param name="handler"></param>
public
void
Subscribe
(
string
subChannel
,
Action
<
RedisChannel
,
RedisValue
>
handler
=
null
)
{
ISubscriber
sub
=
_conn
.
GetSubscriber
();
sub
.
Subscribe
(
subChannel
,
(
channel
,
message
)
=>
{
if
(
handler
==
null
)
{
Console
.
WriteLine
(
subChannel
+
" 订阅收到消息:"
+
message
);
}
else
{
handler
(
channel
,
message
);
}
});
}
/// <summary>
/// 发布
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="channel"></param>
/// <param name="msg"></param>
/// <returns></returns>
public
long
Publish
<
T
>(
string
channel
,
T
msg
)
{
ISubscriber
sub
=
_conn
.
GetSubscriber
();
return
sub
.
Publish
(
channel
,
ConvertJson
(
msg
));
}
/// <summary>
/// 取消订阅
/// </summary>
/// <param name="channel"></param>
public
void
Unsubscribe
(
string
channel
)
{
ISubscriber
sub
=
_conn
.
GetSubscriber
();
sub
.
Unsubscribe
(
channel
);
}
/// <summary>
/// 取消全部订阅
/// </summary>
public
void
UnsubscribeAll
()
{
ISubscriber
sub
=
_conn
.
GetSubscriber
();
sub
.
UnsubscribeAll
();
}
#
endregion
发布订阅
#
region
异步
/// <summary>
/// 订阅
/// </summary>
/// <param name="subChannel"></param>
/// <param name="handler"></param>
public
async
Task
SubscribeAsync
(
string
subChannel
,
Action
<
RedisChannel
,
RedisValue
>
handler
=
null
)
{
ISubscriber
sub
=
_conn
.
GetSubscriber
();
await
sub
.
SubscribeAsync
(
subChannel
,
(
channel
,
message
)
=>
{
if
(
handler
==
null
)
{
Console
.
WriteLine
(
subChannel
+
" 订阅收到消息:"
+
message
);
}
else
{
handler
(
channel
,
message
);
}
});
}
/// <summary>
/// 发布
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="channel"></param>
/// <param name="msg"></param>
/// <returns></returns>
public
async
Task
<
long
>
PublishAsync
<
T
>(
string
channel
,
T
msg
)
{
ISubscriber
sub
=
_conn
.
GetSubscriber
();
return
await
sub
.
PublishAsync
(
channel
,
ConvertJson
(
msg
));
}
/// <summary>
/// 取消订阅
/// </summary>
/// <param name="channel"></param>
public
async
Task
UnsubscribeAsync
(
string
channel
)
{
ISubscriber
sub
=
_conn
.
GetSubscriber
();
await
sub
.
UnsubscribeAsync
(
channel
);
}
/// <summary>
/// 取消全部订阅
/// </summary>
public
async
Task
UnsubscribeAllAsync
()
{
ISubscriber
sub
=
_conn
.
GetSubscriber
();
await
sub
.
UnsubscribeAllAsync
();
}
#
endregion
发布订阅
#
endregion
#
region
其他
/// <summary>
/// 创建事务
/// </summary>
/// <returns></returns>
public
ITransaction
CreateTransaction
()
{
return
GetDatabase
().
CreateTransaction
();
}
/// <summary>
/// 获取数据库
/// </summary>
/// <returns></returns>
public
IDatabase
GetDatabase
()
{
return
_conn
.
GetDatabase
(
DbNum
);
}
/// <summary>
/// 获取服务信息
/// </summary>
/// <param name="hostAndPort"></param>
/// <returns></returns>
public
IServer
GetServer
(
string
hostAndPort
)
{
return
_conn
.
GetServer
(
hostAndPort
);
}
/// <summary>
/// 设置前缀
/// </summary>
/// <param name="customKey"></param>
public
void
SetSysCustomKey
(
string
customKey
)
{
CustomKey
=
customKey
;
}
#
endregion
其他
#
region
辅助方法
private
string
AddSysCustomKey
(
string
oldKey
)
{
var
prefixKey
=
CustomKey
??
RedisConnectionHelp
.
SysCustomKey
;
return
prefixKey
+
oldKey
;
}
private
T
Do
<
T
>(
Func
<
IDatabase
,
T
>
func
)
{
var
database
=
_conn
.
GetDatabase
(
DbNum
);
return
func
(
database
);
}
private
string
ConvertJson
<
T
>(
T
value
)
{
string
result
=
value
is
string
?
value
.
ToString
()
:
JsonConvert
.
SerializeObject
(
value
);
return
result
;
}
private
T
ConvertObj
<
T
>(
RedisValue
value
)
{
if
(
value
.
IsNullOrEmpty
)
return
default
(
T
);
return
JsonConvert
.
DeserializeObject
<
T
>(
value
);
}
private
List
<
T
>
ConvetList
<
T
>(
RedisValue
[]
values
)
{
List
<
T
>
result
=
new
List
<
T
>();
foreach
(
var
item
in
values
)
{
var
model
=
ConvertObj
<
T
>(
item
);
result
.
Add
(
model
);
}
return
result
;
}
private
RedisKey
[]
ConvertRedisKeys
(
List
<
string
>
redisKeys
)
{
return
redisKeys
.
Select
(
redisKey
=>
(
RedisKey
)
redisKey
).
ToArray
();
}
/// <summary>
/// 转化为timespan
/// </summary>
/// <param name="expTime">过期时间,秒</param>
/// <returns></returns>
public
TimeSpan
ToTimeSpan
(
double
expTime
)
{
return
DateTime
.
Now
.
AddSeconds
(
expTime
)
-
DateTime
.
Now
;
}
#
endregion
辅助方法
}
}
Mall.Repository/Core/BaseRepository.cs
0 → 100644
View file @
84fdcc16
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
VT.FW.DB
;
namespace
Mall.Repository
{
public
class
BaseRepository
<
T
>
:
RepositoryBase
<
T
>
where
T
:
class
{
/// <summary>
/// 数据库连接Key
/// </summary>
public
string
ConnKey
{
get
;
set
;
}
public
BaseRepository
(
string
connKey
=
"DefaultConnection"
)
{
try
{
var
classAttribute
=
(
DBAttribute
)
Attribute
.
GetCustomAttribute
(
typeof
(
T
),
typeof
(
DBAttribute
));
connKey
=
classAttribute
.
ConnectionName
??
"DefaultConnection"
;
}
catch
(
Exception
ex
)
{
throw
ex
;
}
ConnKey
=
connKey
;
}
public
string
_ConnectionStr
;
public
string
_ProviderName
;
public
override
string
ConnectionStr
{
get
{
_ConnectionStr
=
Common
.
Config
.
GetConnectionString
(
ConnKey
);
return
_ConnectionStr
;
}
set
{
_ConnectionStr
=
value
;
}
}
public
override
string
ProviderName
{
get
{
_ProviderName
=
"MySql.Data.MySqlClient"
;
return
_ProviderName
;
}
set
{
_ProviderName
=
value
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment