Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
schbrain-parent
Commits
0f444738
Commit
0f444738
authored
Sep 10, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update cache operations
parent
52f6fe8d
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
102 additions
and
142 deletions
+102
-142
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheAutoConfiguration.java
...framework/autoconfigure/cache/CacheAutoConfiguration.java
+7
-7
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheUtils.java
...om/schbrain/framework/autoconfigure/cache/CacheUtils.java
+20
-20
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/CacheOperation.java
...ramework/autoconfigure/cache/provider/CacheOperation.java
+10
-10
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/PrefixedCacheOperation.java
.../autoconfigure/cache/provider/PrefixedCacheOperation.java
+21
-37
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCacheConfiguration.java
...nfigure/cache/provider/redis/RedisCacheConfiguration.java
+4
-4
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCacheOperation.java
...toconfigure/cache/provider/redis/RedisCacheOperation.java
+40
-64
No files found.
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheAutoConfiguration.java
View file @
0f444738
package
com.schbrain.framework.autoconfigure.cache
;
package
com.schbrain.framework.autoconfigure.cache
;
import
com.schbrain.framework.autoconfigure.cache.properties.CacheProperties
;
import
com.schbrain.framework.autoconfigure.cache.properties.CacheProperties
;
import
com.schbrain.framework.autoconfigure.cache.provider.Cache
Provider
;
import
com.schbrain.framework.autoconfigure.cache.provider.Cache
Operation
;
import
com.schbrain.framework.autoconfigure.cache.provider.
CacheProviderDelegate
;
import
com.schbrain.framework.autoconfigure.cache.provider.
PrefixedCacheOperation
;
import
com.schbrain.framework.autoconfigure.cache.provider.redis.RedisCacheConfiguration
;
import
com.schbrain.framework.autoconfigure.cache.provider.redis.RedisCacheConfiguration
;
import
org.springframework.boot.autoconfigure.AutoConfiguration
;
import
org.springframework.boot.autoconfigure.AutoConfiguration
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
...
@@ -23,11 +23,11 @@ import org.springframework.context.annotation.Import;
...
@@ -23,11 +23,11 @@ import org.springframework.context.annotation.Import;
public
class
CacheAutoConfiguration
{
public
class
CacheAutoConfiguration
{
@Bean
@Bean
@ConditionalOnBean
(
Cache
Provider
.
class
)
@ConditionalOnBean
(
Cache
Operation
.
class
)
public
CacheProvider
cacheProvider
(
CacheProvider
cacheProvider
,
CacheProperties
cacheProperties
)
{
public
PrefixedCacheOperation
prefixedCacheOperation
(
CacheOperation
cacheOperation
,
CacheProperties
cacheProperties
)
{
CacheProvider
provider
=
new
CacheProviderDelegate
(
cacheProperties
,
cacheProvider
);
PrefixedCacheOperation
operation
=
new
PrefixedCacheOperation
(
cacheProperties
,
cacheOperation
);
CacheUtils
.
setCache
Provider
(
provider
);
CacheUtils
.
setCache
Operation
(
operation
);
return
provider
;
return
operation
;
}
}
}
}
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheUtils.java
View file @
0f444738
package
com.schbrain.framework.autoconfigure.cache
;
package
com.schbrain.framework.autoconfigure.cache
;
import
com.schbrain.framework.autoconfigure.cache.exception.CacheException
;
import
com.schbrain.framework.autoconfigure.cache.exception.CacheException
;
import
com.schbrain.framework.autoconfigure.cache.provider.
CacheProvider
;
import
com.schbrain.framework.autoconfigure.cache.provider.
PrefixedCacheOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.CollectionUtils
;
...
@@ -12,45 +12,45 @@ import java.util.function.Supplier;
...
@@ -12,45 +12,45 @@ import java.util.function.Supplier;
@Slf4j
@Slf4j
public
class
CacheUtils
{
public
class
CacheUtils
{
private
static
CacheProvider
cacheProvider
;
private
static
PrefixedCacheOperation
cacheOperation
;
public
static
CacheProvider
getCacheProvider
()
{
public
static
PrefixedCacheOperation
getCacheOperation
()
{
if
(
cache
Provider
==
null
)
{
if
(
cache
Operation
==
null
)
{
throw
new
CacheException
(
"Cache
Provider
is null, Please ensure the cache config is correct"
);
throw
new
CacheException
(
"Cache
Operation
is null, Please ensure the cache config is correct"
);
}
}
return
cache
Provider
;
return
cache
Operation
;
}
}
public
static
void
setCache
Provider
(
CacheProvider
cacheProvider
)
{
public
static
void
setCache
Operation
(
PrefixedCacheOperation
cacheOperation
)
{
CacheUtils
.
cache
Provider
=
cacheProvider
;
CacheUtils
.
cache
Operation
=
cacheOperation
;
}
}
/**
/**
* 缓存是否存在
* 缓存是否存在
*/
*/
public
static
boolean
hasKey
(
String
cacheKey
)
{
public
static
boolean
hasKey
(
String
cacheKey
)
{
return
getCache
Provider
().
hasKey
(
cacheKey
);
return
getCache
Operation
().
hasKey
(
cacheKey
);
}
}
/**
/**
* 缓存是否过期
* 缓存是否过期
*/
*/
public
static
boolean
isExpired
(
String
cacheKey
)
{
public
static
boolean
isExpired
(
String
cacheKey
)
{
return
getCache
Provider
().
isExpired
(
cacheKey
);
return
getCache
Operation
().
isExpired
(
cacheKey
);
}
}
/**
/**
* 设置过期时间
* 设置过期时间
*/
*/
public
static
void
expire
(
String
cacheKey
,
Duration
expiration
)
{
public
static
void
expire
(
String
cacheKey
,
Duration
expiration
)
{
getCache
Provider
().
expire
(
cacheKey
,
expiration
);
getCache
Operation
().
expire
(
cacheKey
,
expiration
);
}
}
/**
/**
* 获取过期时间
* 获取过期时间
*/
*/
public
static
Duration
getExpire
(
String
cacheKey
)
{
public
static
Duration
getExpire
(
String
cacheKey
)
{
return
getCache
Provider
().
getExpire
(
cacheKey
);
return
getCache
Operation
().
getExpire
(
cacheKey
);
}
}
/**
/**
...
@@ -64,35 +64,35 @@ public class CacheUtils {
...
@@ -64,35 +64,35 @@ public class CacheUtils {
* 模糊搜索 key, 默认采用 scan 实现
* 模糊搜索 key, 默认采用 scan 实现
*/
*/
public
static
Set
<
String
>
keys
(
String
pattern
,
long
limit
)
{
public
static
Set
<
String
>
keys
(
String
pattern
,
long
limit
)
{
return
getCache
Provider
().
keys
(
pattern
,
limit
);
return
getCache
Operation
().
keys
(
pattern
,
limit
);
}
}
/**
/**
* 获取缓存数据
* 获取缓存数据
*/
*/
public
static
<
T
>
T
getValue
(
String
cacheKey
,
Class
<
T
>
valueType
)
{
public
static
<
T
>
T
getValue
(
String
cacheKey
,
Class
<
T
>
valueType
)
{
return
getCache
Provider
().
get
(
cacheKey
,
valueType
);
return
getCache
Operation
().
getValue
(
cacheKey
,
valueType
);
}
}
/**
/**
* 设置缓存数据
* 设置缓存数据
*/
*/
public
static
<
T
>
void
putValue
(
String
cacheKey
,
T
value
,
Duration
expiration
)
{
public
static
<
T
>
void
putValue
(
String
cacheKey
,
T
value
,
Duration
expiration
)
{
getCache
Provider
().
set
(
cacheKey
,
value
,
expiration
);
getCache
Operation
().
setValue
(
cacheKey
,
value
,
expiration
);
}
}
/**
/**
* 获取缓存数据列表
* 获取缓存数据列表
*/
*/
public
static
<
T
>
List
<
T
>
getList
(
String
cacheKey
,
Class
<
T
>
valueType
)
{
public
static
<
T
>
List
<
T
>
getList
(
String
cacheKey
,
Class
<
T
>
valueType
)
{
return
getCache
Provider
().
getList
(
cacheKey
,
valueType
);
return
getCache
Operation
().
getList
(
cacheKey
,
valueType
);
}
}
/**
/**
* 设置缓存数据列表
* 设置缓存数据列表
*/
*/
public
static
<
T
>
void
putList
(
String
cacheKey
,
List
<
T
>
value
,
Duration
expiration
)
{
public
static
<
T
>
void
putList
(
String
cacheKey
,
List
<
T
>
value
,
Duration
expiration
)
{
getCache
Provider
().
set
(
cacheKey
,
value
,
expiration
);
getCache
Operation
().
setValue
(
cacheKey
,
value
,
expiration
);
}
}
/**
/**
...
@@ -158,14 +158,14 @@ public class CacheUtils {
...
@@ -158,14 +158,14 @@ public class CacheUtils {
* 批量获取
* 批量获取
*/
*/
public
static
<
T
>
Map
<
String
,
T
>
multiGet
(
Collection
<
String
>
cacheKeys
,
Class
<
T
>
valueType
,
boolean
discardIfValueIsNull
)
{
public
static
<
T
>
Map
<
String
,
T
>
multiGet
(
Collection
<
String
>
cacheKeys
,
Class
<
T
>
valueType
,
boolean
discardIfValueIsNull
)
{
return
getCache
Provider
().
multiGet
(
cacheKeys
,
valueType
,
discardIfValueIsNull
);
return
getCache
Operation
().
multiGet
(
cacheKeys
,
valueType
,
discardIfValueIsNull
);
}
}
/**
/**
* 批量设置
* 批量设置
*/
*/
public
static
<
T
>
void
multiSet
(
Map
<
String
,
T
>
data
,
Duration
expiration
)
{
public
static
<
T
>
void
multiSet
(
Map
<
String
,
T
>
data
,
Duration
expiration
)
{
getCache
Provider
().
multiSet
(
data
,
expiration
);
getCache
Operation
().
multiSet
(
data
,
expiration
);
}
}
/**
/**
...
@@ -179,7 +179,7 @@ public class CacheUtils {
...
@@ -179,7 +179,7 @@ public class CacheUtils {
* 删除缓存
* 删除缓存
*/
*/
public
static
void
del
(
List
<
String
>
cacheKeys
)
{
public
static
void
del
(
List
<
String
>
cacheKeys
)
{
getCache
Provider
().
del
(
cacheKeys
);
getCache
Operation
().
del
(
cacheKeys
);
}
}
}
}
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/Cache
Provider
.java
→
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/Cache
Operation
.java
View file @
0f444738
...
@@ -9,8 +9,13 @@ import java.util.Set;
...
@@ -9,8 +9,13 @@ import java.util.Set;
/**
/**
* @author zhuyf
* @author zhuyf
* @since 2020/9/24
* @since 2020/9/24
**/
*/
public
interface
CacheProvider
{
public
interface
CacheOperation
{
/**
* 判断cacheKey是否存在
*/
boolean
hasKey
(
String
cacheKey
);
/**
/**
* 查询key是否过期
* 查询key是否过期
...
@@ -27,11 +32,6 @@ public interface CacheProvider {
...
@@ -27,11 +32,6 @@ public interface CacheProvider {
*/
*/
Duration
getExpire
(
String
cacheKey
);
Duration
getExpire
(
String
cacheKey
);
/**
* 判断cacheKey是否存在
*/
boolean
hasKey
(
String
cacheKey
);
/**
/**
* 模糊搜索 key, 默认采用 scan 实现
* 模糊搜索 key, 默认采用 scan 实现
*/
*/
...
@@ -45,12 +45,12 @@ public interface CacheProvider {
...
@@ -45,12 +45,12 @@ public interface CacheProvider {
/**
/**
* 缓存获取
* 缓存获取
*/
*/
<
T
>
T
get
(
String
cacheKey
,
Class
<
T
>
valueType
);
<
T
>
T
get
Value
(
String
cacheKey
,
Class
<
T
>
valueType
);
/**
/**
* 缓存放入并设置时间
* 缓存放入并设置时间
*/
*/
<
T
>
void
set
(
String
cacheKey
,
T
value
,
Duration
expiration
);
<
T
>
void
set
Value
(
String
cacheKey
,
T
value
,
Duration
expiration
);
/**
/**
* 缓存放入并设置时间
* 缓存放入并设置时间
...
...
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/
CacheProviderDelegate
.java
→
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/
PrefixedCacheOperation
.java
View file @
0f444738
...
@@ -17,14 +17,13 @@ import java.util.Map.Entry;
...
@@ -17,14 +17,13 @@ import java.util.Map.Entry;
* @author liaozan
* @author liaozan
* @since 2022/8/1
* @since 2022/8/1
*/
*/
public
class
CacheProviderDelegate
implements
CacheProvider
{
public
class
PrefixedCacheOperation
{
private
final
CacheOperation
cacheOperation
;
private
final
String
prefixWithDelimiter
;
private
final
String
prefixWithDelimiter
;
private
final
CacheProvider
cacheProvider
;
public
PrefixedCacheOperation
(
CacheProperties
properties
,
CacheOperation
cacheOperation
)
{
this
.
cacheOperation
=
cacheOperation
;
public
CacheProviderDelegate
(
CacheProperties
properties
,
CacheProvider
cacheProvider
)
{
this
.
cacheProvider
=
cacheProvider
;
if
(
properties
.
isAppendPrefix
())
{
if
(
properties
.
isAppendPrefix
())
{
String
prefix
=
properties
.
getPrefix
();
String
prefix
=
properties
.
getPrefix
();
if
(
StringUtils
.
isBlank
(
prefix
))
{
if
(
StringUtils
.
isBlank
(
prefix
))
{
...
@@ -36,54 +35,45 @@ public class CacheProviderDelegate implements CacheProvider {
...
@@ -36,54 +35,45 @@ public class CacheProviderDelegate implements CacheProvider {
}
}
}
}
@Override
public
boolean
hasKey
(
String
cacheKey
)
{
return
cacheOperation
.
hasKey
(
withKeyPrefix
(
cacheKey
));
}
public
boolean
isExpired
(
String
cacheKey
)
{
public
boolean
isExpired
(
String
cacheKey
)
{
return
getCacheProvider
()
.
isExpired
(
withKeyPrefix
(
cacheKey
));
return
cacheOperation
.
isExpired
(
withKeyPrefix
(
cacheKey
));
}
}
@Override
public
void
expire
(
String
cacheKey
,
Duration
expiration
)
{
public
void
expire
(
String
cacheKey
,
Duration
expiration
)
{
checkDuration
(
expiration
);
checkDuration
(
expiration
);
getCacheProvider
()
.
expire
(
withKeyPrefix
(
cacheKey
),
expiration
);
cacheOperation
.
expire
(
withKeyPrefix
(
cacheKey
),
expiration
);
}
}
@Override
public
Duration
getExpire
(
String
cacheKey
)
{
public
Duration
getExpire
(
String
cacheKey
)
{
return
getCacheProvider
()
.
getExpire
(
withKeyPrefix
(
cacheKey
));
return
cacheOperation
.
getExpire
(
withKeyPrefix
(
cacheKey
));
}
}
@Override
public
boolean
hasKey
(
String
cacheKey
)
{
return
getCacheProvider
().
hasKey
(
withKeyPrefix
(
cacheKey
));
}
@Override
public
Set
<
String
>
keys
(
String
pattern
,
long
limit
)
{
public
Set
<
String
>
keys
(
String
pattern
,
long
limit
)
{
Set
<
String
>
keys
=
getCacheProvider
()
.
keys
(
withKeyPrefix
(
pattern
),
limit
);
Set
<
String
>
keys
=
cacheOperation
.
keys
(
withKeyPrefix
(
pattern
),
limit
);
return
StreamUtils
.
toSet
(
keys
,
this
::
removeKeyPrefix
);
return
StreamUtils
.
toSet
(
keys
,
this
::
removeKeyPrefix
);
}
}
@Override
public
void
del
(
List
<
String
>
cacheKeys
)
{
public
void
del
(
List
<
String
>
cacheKeys
)
{
if
(
CollectionUtils
.
isEmpty
(
cacheKeys
))
{
if
(
CollectionUtils
.
isEmpty
(
cacheKeys
))
{
return
;
return
;
}
}
List
<
String
>
keysWithPrefix
=
StreamUtils
.
toList
(
cacheKeys
,
this
::
withKeyPrefix
);
List
<
String
>
keysWithPrefix
=
StreamUtils
.
toList
(
cacheKeys
,
this
::
withKeyPrefix
);
getCacheProvider
()
.
del
(
keysWithPrefix
);
cacheOperation
.
del
(
keysWithPrefix
);
}
}
@Override
public
<
T
>
T
getValue
(
String
cacheKey
,
Class
<
T
>
valueType
)
{
public
<
T
>
T
get
(
String
cacheKey
,
Class
<
T
>
valueType
)
{
return
cacheOperation
.
getValue
(
withKeyPrefix
(
cacheKey
),
valueType
);
return
getCacheProvider
().
get
(
withKeyPrefix
(
cacheKey
),
valueType
);
}
}
@Override
public
<
T
>
void
setValue
(
String
cacheKey
,
T
value
,
Duration
expiration
)
{
public
<
T
>
void
set
(
String
cacheKey
,
T
value
,
Duration
expiration
)
{
checkDuration
(
expiration
);
checkDuration
(
expiration
);
getCacheProvider
().
set
(
withKeyPrefix
(
cacheKey
),
value
,
expiration
);
cacheOperation
.
setValue
(
withKeyPrefix
(
cacheKey
),
value
,
expiration
);
}
}
@Override
public
<
T
>
void
multiSet
(
Map
<
String
,
T
>
data
,
Duration
expiration
)
{
public
<
T
>
void
multiSet
(
Map
<
String
,
T
>
data
,
Duration
expiration
)
{
if
(
MapUtils
.
isEmpty
(
data
))
{
if
(
MapUtils
.
isEmpty
(
data
))
{
return
;
return
;
...
@@ -93,16 +83,15 @@ public class CacheProviderDelegate implements CacheProvider {
...
@@ -93,16 +83,15 @@ public class CacheProviderDelegate implements CacheProvider {
for
(
Entry
<
String
,
T
>
entry
:
data
.
entrySet
())
{
for
(
Entry
<
String
,
T
>
entry
:
data
.
entrySet
())
{
dataWithPrefixedKey
.
put
(
withKeyPrefix
(
entry
.
getKey
()),
entry
.
getValue
());
dataWithPrefixedKey
.
put
(
withKeyPrefix
(
entry
.
getKey
()),
entry
.
getValue
());
}
}
getCacheProvider
()
.
multiSet
(
dataWithPrefixedKey
,
expiration
);
cacheOperation
.
multiSet
(
dataWithPrefixedKey
,
expiration
);
}
}
@Override
public
<
T
>
Map
<
String
,
T
>
multiGet
(
Collection
<
String
>
cacheKeys
,
Class
<
T
>
valueType
,
boolean
discardIfValueIsNull
)
{
public
<
T
>
Map
<
String
,
T
>
multiGet
(
Collection
<
String
>
cacheKeys
,
Class
<
T
>
valueType
,
boolean
discardIfValueIsNull
)
{
if
(
CollectionUtils
.
isEmpty
(
cacheKeys
))
{
if
(
CollectionUtils
.
isEmpty
(
cacheKeys
))
{
return
Collections
.
emptyMap
();
return
Collections
.
emptyMap
();
}
}
List
<
String
>
keysWithPrefix
=
StreamUtils
.
toList
(
cacheKeys
,
this
::
withKeyPrefix
);
List
<
String
>
keysWithPrefix
=
StreamUtils
.
toList
(
cacheKeys
,
this
::
withKeyPrefix
);
Map
<
String
,
T
>
dataWithPrefixedKey
=
getCacheProvider
()
.
multiGet
(
keysWithPrefix
,
valueType
,
discardIfValueIsNull
);
Map
<
String
,
T
>
dataWithPrefixedKey
=
cacheOperation
.
multiGet
(
keysWithPrefix
,
valueType
,
discardIfValueIsNull
);
if
(
MapUtils
.
isEmpty
(
dataWithPrefixedKey
))
{
if
(
MapUtils
.
isEmpty
(
dataWithPrefixedKey
))
{
return
Collections
.
emptyMap
();
return
Collections
.
emptyMap
();
}
else
{
}
else
{
...
@@ -115,13 +104,8 @@ public class CacheProviderDelegate implements CacheProvider {
...
@@ -115,13 +104,8 @@ public class CacheProviderDelegate implements CacheProvider {
}
}
}
}
@Override
public
<
T
>
List
<
T
>
getList
(
String
cacheKey
,
Class
<
T
>
valueType
)
{
public
<
T
>
List
<
T
>
getList
(
String
cacheKey
,
Class
<
T
>
valueType
)
{
return
getCacheProvider
().
getList
(
withKeyPrefix
(
cacheKey
),
valueType
);
return
cacheOperation
.
getList
(
withKeyPrefix
(
cacheKey
),
valueType
);
}
public
CacheProvider
getCacheProvider
()
{
return
cacheProvider
;
}
}
protected
String
withKeyPrefix
(
String
cacheKey
)
{
protected
String
withKeyPrefix
(
String
cacheKey
)
{
...
...
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCacheConfiguration.java
View file @
0f444738
...
@@ -21,10 +21,10 @@ public class RedisCacheConfiguration {
...
@@ -21,10 +21,10 @@ public class RedisCacheConfiguration {
@Bean
@Bean
@ConditionalOnBean
(
RedisConnectionFactory
.
class
)
@ConditionalOnBean
(
RedisConnectionFactory
.
class
)
@ConditionalOnMissingBean
(
RedisCache
Provider
.
class
)
@ConditionalOnMissingBean
(
RedisCache
Operation
.
class
)
public
RedisCache
Provider
redisCacheProvider
(
RedisConnectionFactory
redisConnectionF
actory
,
ObjectProvider
<
StringRedisTemplate
>
redisTemplate
)
{
public
RedisCache
Operation
redisCacheProvider
(
RedisConnectionFactory
f
actory
,
ObjectProvider
<
StringRedisTemplate
>
redisTemplate
)
{
StringRedisTemplate
stringRedisTemplate
=
redisTemplate
.
getIfAvailable
(()
->
new
StringRedisTemplate
(
redisConnectionF
actory
));
StringRedisTemplate
stringRedisTemplate
=
redisTemplate
.
getIfAvailable
(()
->
new
StringRedisTemplate
(
f
actory
));
return
new
RedisCache
Provider
(
stringRedisTemplate
);
return
new
RedisCache
Operation
(
stringRedisTemplate
);
}
}
}
}
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCache
Provider
.java
→
starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCache
Operation
.java
View file @
0f444738
...
@@ -4,8 +4,7 @@ import com.google.common.collect.Iterables;
...
@@ -4,8 +4,7 @@ import com.google.common.collect.Iterables;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Maps
;
import
com.schbrain.common.util.JacksonUtils
;
import
com.schbrain.common.util.JacksonUtils
;
import
com.schbrain.framework.autoconfigure.cache.exception.CacheException
;
import
com.schbrain.framework.autoconfigure.cache.exception.CacheException
;
import
com.schbrain.framework.autoconfigure.cache.provider.CacheProvider
;
import
com.schbrain.framework.autoconfigure.cache.provider.CacheOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.data.redis.core.Cursor
;
import
org.springframework.data.redis.core.Cursor
;
import
org.springframework.data.redis.core.RedisCallback
;
import
org.springframework.data.redis.core.RedisCallback
;
...
@@ -22,37 +21,32 @@ import java.util.concurrent.TimeUnit;
...
@@ -22,37 +21,32 @@ import java.util.concurrent.TimeUnit;
/**
/**
* @author zhuyf
* @author zhuyf
* @since 2022/7/25
* @since 2022/7/25
**/
*/
@Slf4j
public
class
RedisCacheOperation
implements
CacheOperation
{
public
class
RedisCacheProvider
implements
CacheProvider
{
private
static
final
int
DEFAULT_BATCH_SIZE
=
100
;
private
static
final
int
DEFAULT_BATCH_SIZE
=
100
;
private
final
StringRedisTemplate
redisTemplate
;
private
final
StringRedisTemplate
redisTemplate
;
public
RedisCache
Provider
(
StringRedisTemplate
redisTemplate
)
{
public
RedisCache
Operation
(
StringRedisTemplate
redisTemplate
)
{
this
.
redisTemplate
=
redisTemplate
;
this
.
redisTemplate
=
redisTemplate
;
}
}
/**
@Override
* 查询key是否过期
public
boolean
hasKey
(
String
cacheKey
)
{
*/
return
Boolean
.
TRUE
.
equals
(
redisTemplate
.
hasKey
(
cacheKey
));
}
@Override
@Override
public
boolean
isExpired
(
String
cacheKey
)
{
public
boolean
isExpired
(
String
cacheKey
)
{
return
!
hasKey
(
cacheKey
);
return
!
hasKey
(
cacheKey
);
}
}
/**
* 指定缓存失效时间
*/
@Override
@Override
public
void
expire
(
String
key
,
Duration
expiration
)
{
public
void
expire
(
String
key
,
Duration
expiration
)
{
redisTemplate
.
expire
(
key
,
expiration
.
toMillis
(),
TimeUnit
.
MILLISECONDS
);
redisTemplate
.
expire
(
key
,
expiration
.
toMillis
(),
TimeUnit
.
MILLISECONDS
);
}
}
/**
* 根据key 获取过期时间
*/
@Override
@Override
public
Duration
getExpire
(
String
key
)
{
public
Duration
getExpire
(
String
key
)
{
Long
expiration
=
redisTemplate
.
getExpire
(
key
,
TimeUnit
.
MILLISECONDS
);
Long
expiration
=
redisTemplate
.
getExpire
(
key
,
TimeUnit
.
MILLISECONDS
);
...
@@ -62,17 +56,6 @@ public class RedisCacheProvider implements CacheProvider {
...
@@ -62,17 +56,6 @@ public class RedisCacheProvider implements CacheProvider {
return
Duration
.
ofMillis
(
expiration
);
return
Duration
.
ofMillis
(
expiration
);
}
}
/**
* 判断key是否存在
*/
@Override
public
boolean
hasKey
(
String
cacheKey
)
{
return
Boolean
.
TRUE
.
equals
(
redisTemplate
.
hasKey
(
cacheKey
));
}
/**
* 模糊搜索 key
*/
@Override
@Override
public
Set
<
String
>
keys
(
String
pattern
,
long
limit
)
{
public
Set
<
String
>
keys
(
String
pattern
,
long
limit
)
{
Set
<
String
>
keys
=
new
HashSet
<>();
Set
<
String
>
keys
=
new
HashSet
<>();
...
@@ -88,9 +71,6 @@ public class RedisCacheProvider implements CacheProvider {
...
@@ -88,9 +71,6 @@ public class RedisCacheProvider implements CacheProvider {
return
keys
;
return
keys
;
}
}
/**
* 删除缓存
*/
@Override
@Override
public
void
del
(
List
<
String
>
cacheKeys
)
{
public
void
del
(
List
<
String
>
cacheKeys
)
{
if
(
CollectionUtils
.
isEmpty
(
cacheKeys
))
{
if
(
CollectionUtils
.
isEmpty
(
cacheKeys
))
{
...
@@ -99,61 +79,57 @@ public class RedisCacheProvider implements CacheProvider {
...
@@ -99,61 +79,57 @@ public class RedisCacheProvider implements CacheProvider {
redisTemplate
.
delete
(
cacheKeys
);
redisTemplate
.
delete
(
cacheKeys
);
}
}
/**
* 缓存获取
*/
@Override
@Override
public
<
T
>
T
get
(
String
cacheKey
,
Class
<
T
>
type
)
{
public
<
T
>
T
get
Value
(
String
cacheKey
,
Class
<
T
>
type
)
{
return
JacksonUtils
.
getObjectFromJson
(
getValueFromRedis
(
cacheKey
),
type
);
return
JacksonUtils
.
getObjectFromJson
(
getValueFromRedis
(
cacheKey
),
type
);
}
}
/**
* 普通缓存放入并设置时间
*/
@Override
@Override
public
<
T
>
void
set
(
String
cacheKey
,
T
value
,
Duration
expiration
)
{
public
<
T
>
void
set
Value
(
String
cacheKey
,
T
value
,
Duration
expiration
)
{
setValueToRedis
(
cacheKey
,
value
,
expiration
);
setValueToRedis
(
cacheKey
,
value
,
expiration
);
}
}
@Override
@Override
public
<
T
>
void
multiSet
(
Map
<
String
,
T
>
data
,
Duration
expiration
)
{
public
<
T
>
void
multiSet
(
Map
<
String
,
T
>
data
,
Duration
expiration
)
{
Iterables
.
partition
(
data
.
keySet
(),
DEFAULT_BATCH_SIZE
).
forEach
(
keys
->
Iterables
.
partition
(
data
.
keySet
(),
DEFAULT_BATCH_SIZE
).
forEach
(
keys
->
redisTemplate
.
executePipelined
(
multiSet
(
data
,
expiration
,
keys
)));
redisTemplate
.
executePipelined
((
RedisCallback
<
Void
>)
connection
->
{
Map
<
byte
[],
byte
[]>
byteMap
=
Maps
.
newHashMapWithExpectedSize
(
keys
.
size
());
for
(
String
key
:
keys
)
{
byteMap
.
put
(
key
.
getBytes
(
StandardCharsets
.
UTF_8
),
JacksonUtils
.
writeObjectAsBytes
(
data
.
get
(
key
)));
}
connection
.
mSet
(
byteMap
);
long
expirationMillis
=
expiration
.
toMillis
();
for
(
byte
[]
rawKey
:
byteMap
.
keySet
())
{
connection
.
pExpire
(
rawKey
,
expirationMillis
);
}
return
null
;
}));
}
}
@Override
@Override
public
<
T
>
Map
<
String
,
T
>
multiGet
(
Collection
<
String
>
cacheKeys
,
Class
<
T
>
type
,
boolean
discardIfValueIsNull
)
{
public
<
T
>
Map
<
String
,
T
>
multiGet
(
Collection
<
String
>
cacheKeys
,
Class
<
T
>
type
,
boolean
discardIfValueIsNull
)
{
Map
<
String
,
T
>
result
=
Maps
.
newHashMapWithExpectedSize
(
cacheKeys
.
size
());
Map
<
String
,
T
>
result
=
Maps
.
newHashMapWithExpectedSize
(
cacheKeys
.
size
());
Iterables
.
partition
(
cacheKeys
,
DEFAULT_BATCH_SIZE
).
forEach
(
subKeys
->
{
Iterables
.
partition
(
cacheKeys
,
DEFAULT_BATCH_SIZE
).
forEach
(
subKeys
->
multiGet
(
type
,
discardIfValueIsNull
,
subKeys
,
result
));
List
<
String
>
valueList
=
Objects
.
requireNonNull
(
redisTemplate
.
opsForValue
().
multiGet
(
subKeys
));
return
result
;
for
(
int
i
=
0
;
i
<
subKeys
.
size
();
i
++)
{
}
@Override
public
<
T
>
List
<
T
>
getList
(
String
cacheKey
,
Class
<
T
>
type
)
{
return
JacksonUtils
.
getListFromJson
(
getValueFromRedis
(
cacheKey
),
type
);
}
private
<
T
>
void
multiGet
(
Class
<
T
>
type
,
boolean
discardIfValueIsNull
,
List
<
String
>
keys
,
Map
<
String
,
T
>
result
)
{
List
<
String
>
valueList
=
Objects
.
requireNonNull
(
redisTemplate
.
opsForValue
().
multiGet
(
keys
));
for
(
int
i
=
0
;
i
<
keys
.
size
();
i
++)
{
T
rawValue
=
JacksonUtils
.
getObjectFromJson
(
valueList
.
get
(
i
),
type
);
T
rawValue
=
JacksonUtils
.
getObjectFromJson
(
valueList
.
get
(
i
),
type
);
if
(
discardIfValueIsNull
&&
rawValue
==
null
)
{
if
(
discardIfValueIsNull
&&
rawValue
==
null
)
{
continue
;
continue
;
}
}
result
.
put
(
subK
eys
.
get
(
i
),
rawValue
);
result
.
put
(
k
eys
.
get
(
i
),
rawValue
);
}
}
});
return
result
;
}
}
/**
private
<
T
>
RedisCallback
<
Void
>
multiSet
(
Map
<
String
,
T
>
data
,
Duration
expiration
,
List
<
String
>
keys
)
{
* list 缓存获取
return
connection
->
{
*/
Map
<
byte
[],
byte
[]>
byteMap
=
Maps
.
newHashMapWithExpectedSize
(
keys
.
size
());
@Override
for
(
String
key
:
keys
)
{
public
<
T
>
List
<
T
>
getList
(
String
cacheKey
,
Class
<
T
>
type
)
{
byteMap
.
put
(
key
.
getBytes
(
StandardCharsets
.
UTF_8
),
JacksonUtils
.
writeObjectAsBytes
(
data
.
get
(
key
)));
return
JacksonUtils
.
getListFromJson
(
getValueFromRedis
(
cacheKey
),
type
);
}
connection
.
mSet
(
byteMap
);
long
expirationMillis
=
expiration
.
toMillis
();
for
(
byte
[]
rawKey
:
byteMap
.
keySet
())
{
connection
.
pExpire
(
rawKey
,
expirationMillis
);
}
return
null
;
};
}
}
private
String
getValueFromRedis
(
String
cacheKey
)
{
private
String
getValueFromRedis
(
String
cacheKey
)
{
...
...
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