Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
caopu16
whatsmars
提交
3fb0e985
W
whatsmars
项目概览
caopu16
/
whatsmars
与 Fork 源项目一致
Fork自
武汉红喜 / whatsmars
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
whatsmars
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3fb0e985
编写于
12月 30, 2018
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
RedisService封装简化
上级
f337413c
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
267 addition
and
775 deletion
+267
-775
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/RedisTemplate.java
...java/org/hongxi/whatsmars/redis/client/RedisTemplate.java
+2
-2
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisListService.java
...ngxi/whatsmars/redis/client/service/RedisListService.java
+0
-16
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisMapService.java
...ongxi/whatsmars/redis/client/service/RedisMapService.java
+0
-28
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisService.java
...g/hongxi/whatsmars/redis/client/service/RedisService.java
+57
-116
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisSortedSetService.java
...whatsmars/redis/client/service/RedisSortedSetService.java
+0
-18
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisStringService.java
...xi/whatsmars/redis/client/service/RedisStringService.java
+0
-48
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisListServiceImpl.java
...smars/redis/client/service/impl/RedisListServiceImpl.java
+0
-57
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisMapServiceImpl.java
...tsmars/redis/client/service/impl/RedisMapServiceImpl.java
+0
-157
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisServiceImpl.java
...whatsmars/redis/client/service/impl/RedisServiceImpl.java
+187
-155
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisSortedSetServiceImpl.java
.../redis/client/service/impl/RedisSortedSetServiceImpl.java
+0
-58
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisStringServiceImpl.java
...ars/redis/client/service/impl/RedisStringServiceImpl.java
+0
-116
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/controller/InitRunner.java
...g/hongxi/whatsmars/spring/boot/controller/InitRunner.java
+21
-4
未找到文件。
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/RedisTemplate.java
浏览文件 @
3fb0e985
...
...
@@ -32,10 +32,10 @@ public class RedisTemplate {
}
}
public
void
set
(
String
key
,
String
value
,
int
activeTime
)
{
public
void
set
(
String
key
,
String
value
,
int
seconds
)
{
ShardedJedis
jedis
=
fetchJedis
();
try
{
jedis
.
setex
(
key
,
activeTime
,
value
);
jedis
.
setex
(
key
,
seconds
,
value
);
}
finally
{
release
(
jedis
);
}
...
...
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisListService.java
已删除
100644 → 0
浏览文件 @
f337413c
package
org.hongxi.whatsmars.redis.client.service
;
import
java.util.List
;
public
interface
RedisListService
{
long
rpush
(
byte
[]
key
,
byte
[]...
values
);
long
rpush
(
String
key
,
byte
[]...
values
);
List
<
byte
[]>
lrange
(
byte
[]
key
,
long
begin
,
long
end
);
List
<
byte
[]>
lrange
(
String
key
,
long
begin
,
long
end
);
List
<
byte
[]>
lrange
(
String
key
);
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisMapService.java
已删除
100644 → 0
浏览文件 @
f337413c
package
org.hongxi.whatsmars.redis.client.service
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
public
interface
RedisMapService
{
boolean
hMSet
(
String
redisKey
,
Map
<
byte
[],
byte
[]>
valueMap
);
Set
<
byte
[]>
hKeys
(
String
redisKey
);
Map
<
byte
[],
byte
[]>
hGetAll
(
String
redisKey
);
List
<
byte
[]>
hMGet
(
String
redisKey
,
List
<
String
>
mapKeys
);
byte
[]
hGet
(
String
redisKey
,
String
mapKey
);
long
hDel
(
String
redisKey
,
List
<
String
>
mapKeys
);
long
hDel
(
String
redisKey
,
byte
[]
mapKey
);
boolean
hSet
(
String
redisKey
,
String
mapKey
,
String
mapValue
);
String
hGetString
(
String
redisKey
,
String
mapKey
);
String
hGet
(
String
redisKey
,
byte
[]
mapKey
);
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisService.java
浏览文件 @
3fb0e985
...
...
@@ -2,122 +2,63 @@ package org.hongxi.whatsmars.redis.client.service;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
public
interface
RedisService
<
T
>
{
/**
* <pre>
*
* 通过key删除
* @param keys
* @return 被删除的记录数
* </pre>
*/
long
delete
(
String
...
keys
);
/**
* <pre>
*
* 通过keys删除
* @param keys
* @return 被删除的记录数
* </pre>
*/
long
delete
(
Collection
<
String
>
keys
);
/**
* <pre>
*
* @param key
* @param value
* @param activeTime 秒
* @return 添加key value 并且设置存活时间
* </pre>
*/
boolean
set
(
byte
[]
key
,
byte
[]
value
,
long
activeTime
);
/**
* <pre>
*
* @param key
* @param value
* @param activeTime 秒
* @return 添加key value 并且设置存活时间
* </pre>
*/
boolean
set
(
String
key
,
String
value
,
long
activeTime
);
/**
* <pre>
*
* @param key
* @param value
* @return 添加key value
* </pre>
*/
boolean
set
(
String
key
,
String
value
);
/**
* <pre>
*
* @param key
* @param value
* @return 添加key value
* </pre>
*/
boolean
set
(
byte
[]
key
,
byte
[]
value
);
boolean
set
(
byte
[]
key
,
T
value
,
long
activeTime
);
boolean
set
(
String
key
,
T
value
,
long
activeTime
);
boolean
set
(
String
key
,
T
value
);
/**
* <pre>
*
* @param key
* @return 获得value
* </pre>
*/
String
get
(
String
key
);
T
getObject
(
String
key
,
Class
<
T
>
c
);
byte
[]
getBytes
(
String
key
);
/**
* <pre>
*
* @param pattern
* @return 通过正则匹配keys
* </pre>
*/
Set
<
String
>
matchKeys
(
String
pattern
);
/**
* <pre>
*
* @param key
* @return 检查key是否已经存在
* </pre>
*/
boolean
exists
(
String
key
);
/**
* <pre>
*
* @return 清空所有数据
* </pre>
*/
boolean
flushDB
();
/**
* <pre>
*
* @param keys
* @return 批量获取key的值
* </pre>
*/
List
<
T
>
multiGet
(
Collection
keys
);
/**
* Created by shenhongxi on 2018/12/24.
*/
public
interface
RedisService
{
void
set
(
String
key
,
String
value
);
void
set
(
String
key
,
String
value
,
long
seconds
);
void
setIfAbsent
(
String
key
,
String
value
);
String
get
(
String
key
);
String
getAndSet
(
String
key
,
String
value
);
void
multiSet
(
Map
<
String
,
String
>
map
);
List
<
String
>
multiGet
(
Collection
<
String
>
keys
);
Long
increment
(
String
key
,
long
delta
);
Double
increment
(
String
key
,
double
delta
);
Integer
append
(
String
key
,
String
value
);
String
get
(
String
key
,
long
start
,
long
end
);
Long
size
(
String
key
);
Boolean
setBit
(
String
key
,
long
offset
,
boolean
value
);
Boolean
getBit
(
String
key
,
long
offset
);
long
sadd
(
String
key
,
String
...
values
);
Set
<
String
>
smembers
(
String
key
);
void
convertAndSend
(
String
channel
,
Object
obj
);
void
delete
(
String
key
);
boolean
exists
(
String
key
);
Set
<
String
>
matchKeys
(
String
pattern
);
<
T
>
void
set
(
String
key
,
T
value
,
long
seconds
)
throws
Exception
;
<
T
>
void
set
(
String
key
,
T
value
)
throws
Exception
;
<
T
>
T
get
(
String
key
,
Class
<
T
>
clazz
);
boolean
set
(
byte
[]
key
,
byte
[]
value
,
long
seconds
);
boolean
set
(
byte
[]
key
,
byte
[]
value
);
byte
[]
getBytes
(
String
key
);
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisSortedSetService.java
已删除
100644 → 0
浏览文件 @
f337413c
package
org.hongxi.whatsmars.redis.client.service
;
import
org.springframework.data.redis.connection.RedisZSetCommands.Tuple
;
import
java.util.Set
;
public
interface
RedisSortedSetService
{
long
zadd
(
String
key
,
Set
<
Tuple
>
tuples
);
long
zadd
(
byte
[]
key
,
Set
<
Tuple
>
tuples
);
Set
<
byte
[]>
zrange
(
byte
[]
key
,
int
start
,
int
end
);
Set
<
byte
[]>
zrange
(
String
key
);
Set
<
byte
[]>
zrange
(
String
key
,
int
start
,
int
end
);
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisStringService.java
已删除
100644 → 0
浏览文件 @
f337413c
package
org.hongxi.whatsmars.redis.client.service
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
/**
* Created by shenhongxi on 2018/12/24.
*/
public
interface
RedisStringService
{
void
set
(
String
key
,
String
value
);
void
set
(
String
key
,
String
value
,
long
activeTime
);
void
setIfAbsent
(
String
key
,
String
value
);
String
get
(
String
key
);
String
getAndSet
(
String
key
,
String
value
);
void
multiSet
(
Map
<
String
,
String
>
map
);
List
<
String
>
multiGet
(
Collection
<
String
>
keys
);
Long
increment
(
String
key
,
long
delta
);
Double
increment
(
String
key
,
double
delta
);
Integer
append
(
String
key
,
String
value
);
String
get
(
String
key
,
long
start
,
long
end
);
Long
size
(
String
key
);
Boolean
setBit
(
String
key
,
long
offset
,
boolean
value
);
Boolean
getBit
(
String
key
,
long
offset
);
long
sadd
(
String
key
,
String
...
values
);
Set
<
String
>
smembers
(
String
key
);
void
convertAndSend
(
String
channel
,
Object
obj
);
void
delete
(
String
key
);
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisListServiceImpl.java
已删除
100644 → 0
浏览文件 @
f337413c
package
org.hongxi.whatsmars.redis.client.service.impl
;
import
org.hongxi.whatsmars.redis.client.service.RedisListService
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.data.redis.connection.RedisConnection
;
import
org.springframework.data.redis.core.RedisCallback
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
java.util.List
;
@Service
public
class
RedisListServiceImpl
implements
RedisListService
{
@Resource
private
RedisTemplate
<
String
,
List
<
Object
>>
redisTemplate
;
@Override
public
long
rpush
(
byte
[]
key
,
byte
[]...
values
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Long
>()
{
@Override
public
Long
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
rPush
(
key
,
values
);
}
});
}
@Override
public
long
rpush
(
String
key
,
byte
[]...
values
)
{
if
(
values
==
null
||
values
.
length
<=
0
){
return
0
;
}
return
this
.
rpush
(
key
.
getBytes
(),
values
);
}
@Override
public
List
<
byte
[]>
lrange
(
byte
[]
key
,
long
begin
,
long
end
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
List
<
byte
[]>>()
{
@Override
public
List
<
byte
[]>
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
lRange
(
key
,
begin
,
end
);
}
});
}
@Override
public
List
<
byte
[]>
lrange
(
String
key
,
long
begin
,
long
end
)
{
return
this
.
lrange
(
key
.
getBytes
(),
begin
,
end
);
}
@Override
public
List
<
byte
[]>
lrange
(
String
key
)
{
return
this
.
lrange
(
key
,
0
,
-
1
);
}
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisMapServiceImpl.java
已删除
100644 → 0
浏览文件 @
f337413c
package
org.hongxi.whatsmars.redis.client.service.impl
;
import
lombok.extern.slf4j.Slf4j
;
import
org.hongxi.whatsmars.redis.client.service.RedisMapService
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.data.redis.connection.RedisConnection
;
import
org.springframework.data.redis.core.RedisCallback
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.CollectionUtils
;
import
javax.annotation.Resource
;
import
java.io.UnsupportedEncodingException
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
@Slf4j
@Service
public
class
RedisMapServiceImpl
implements
RedisMapService
{
private
static
final
String
CHARSET
=
"UTF8"
;
@Resource
private
RedisTemplate
<
String
,
Map
<
byte
[],
byte
[]>>
redisTemplate
;
@Override
public
boolean
hMSet
(
String
redisKey
,
Map
<
byte
[],
byte
[]>
valueMap
)
{
if
(
CollectionUtils
.
isEmpty
(
valueMap
)){
return
false
;
}
redisTemplate
.
execute
(
new
RedisCallback
<
Boolean
>()
{
@Override
public
Boolean
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
connection
.
hMSet
(
redisKey
.
getBytes
(),
valueMap
);
return
true
;
}
});
return
false
;
}
@Override
public
Set
<
byte
[]>
hKeys
(
String
redisKey
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Set
<
byte
[]>>()
{
@Override
public
Set
<
byte
[]>
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
hKeys
(
redisKey
.
getBytes
());
}
});
}
@Override
public
Map
<
byte
[],
byte
[]>
hGetAll
(
String
redisKey
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Map
<
byte
[],
byte
[]>>()
{
@Override
public
Map
<
byte
[],
byte
[]>
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
hGetAll
(
redisKey
.
getBytes
());
}
});
}
@Override
public
List
<
byte
[]>
hMGet
(
String
redisKey
,
List
<
String
>
mapKeys
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
List
<
byte
[]>>()
{
@Override
public
List
<
byte
[]>
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
byte
[][]
keys
=
new
byte
[
mapKeys
.
size
()][];
for
(
int
i
=
0
;
i
<
mapKeys
.
size
();
i
++)
{
keys
[
i
]
=
mapKeys
.
get
(
i
).
getBytes
();
}
return
connection
.
hMGet
(
redisKey
.
getBytes
(),
keys
);
}
});
}
@Override
public
byte
[]
hGet
(
String
redisKey
,
String
mapKey
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
byte
[]>()
{
@Override
public
byte
[]
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
hGet
(
redisKey
.
getBytes
(),
mapKey
.
getBytes
());
}
});
}
@Override
public
long
hDel
(
String
redisKey
,
List
<
String
>
mapKeys
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Long
>()
{
@Override
public
Long
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
byte
[][]
keys
=
new
byte
[
mapKeys
.
size
()][];
for
(
int
i
=
0
;
i
<
mapKeys
.
size
();
i
++)
{
keys
[
i
]
=
mapKeys
.
get
(
i
).
getBytes
();
}
return
connection
.
hDel
(
redisKey
.
getBytes
(),
keys
);
}
});
}
@Override
public
boolean
hSet
(
String
redisKey
,
String
mapKey
,
String
mapValue
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Boolean
>()
{
@Override
public
Boolean
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
hSet
(
redisKey
.
getBytes
(),
mapKey
.
getBytes
(),
mapValue
.
getBytes
());
}
});
}
@Override
public
String
hGetString
(
String
redisKey
,
String
mapKey
)
{
byte
[]
value
=
hGet
(
redisKey
,
mapKey
);
try
{
return
value
==
null
?
null
:
new
String
(
value
,
CHARSET
);
}
catch
(
UnsupportedEncodingException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
@Override
public
String
hGet
(
String
redisKey
,
byte
[]
mapKey
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
String
>()
{
@Override
public
String
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
byte
[]
value
=
connection
.
hGet
(
redisKey
.
getBytes
(),
mapKey
);
if
(
null
==
value
){
return
null
;
}
return
new
String
(
value
);
}
});
}
@Override
public
long
hDel
(
String
redisKey
,
byte
[]
mapKey
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Long
>()
{
@Override
public
Long
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
hDel
(
redisKey
.
getBytes
(),
mapKey
);
}
});
}
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisServiceImpl.java
浏览文件 @
3fb0e985
package
org.hongxi.whatsmars.redis.client.service.impl
;
import
com.
fasterxml.jackson.core.JsonProcessingException
;
import
com.
alibaba.fastjson.JSON
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
lombok.extern.slf4j.Slf4j
;
import
org.hongxi.whatsmars.redis.client.service.RedisService
;
...
...
@@ -8,169 +8,201 @@ import org.springframework.dao.DataAccessException;
import
org.springframework.data.redis.connection.RedisConnection
;
import
org.springframework.data.redis.core.RedisCallback
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
java.io.Serializable
;
import
java.io.UnsupportedEncodingException
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.TimeUnit
;
/**
* Created by shenhongxi on 2018/12/24.
*/
@Slf4j
@Service
public
class
RedisServiceImpl
<
T
>
implements
RedisService
<
T
>
{
private
static
final
String
CHARSET
=
"UTF8"
;
@Resource
private
RedisTemplate
<
String
,
Serializable
>
redisTemplate
;
@Override
public
boolean
set
(
byte
[]
key
,
byte
[]
value
,
long
activeTime
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Boolean
>()
{
public
Boolean
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
boolean
rs
=
true
;
connection
.
set
(
key
,
value
);
if
(
activeTime
>
0
)
{
rs
=
connection
.
expire
(
key
,
activeTime
);
}
return
rs
;
}
});
}
@Override
public
boolean
set
(
String
key
,
String
value
,
long
activeTime
)
{
return
this
.
set
(
key
.
getBytes
(),
value
.
getBytes
(),
activeTime
);
}
@Override
public
boolean
set
(
String
key
,
String
value
)
{
return
this
.
set
(
key
,
value
,
0L
);
}
@Override
public
boolean
set
(
byte
[]
key
,
byte
[]
value
)
{
return
this
.
set
(
key
,
value
,
0L
);
}
@Override
public
boolean
set
(
byte
[]
key
,
T
value
,
long
activeTime
){
ObjectMapper
objectMapper
=
new
ObjectMapper
();
byte
[]
b
=
null
;
try
{
b
=
objectMapper
.
writeValueAsBytes
(
value
);
}
catch
(
JsonProcessingException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
return
false
;
}
return
this
.
set
(
key
,
b
,
activeTime
);
}
@Override
public
boolean
set
(
String
key
,
T
value
,
long
activeTime
)
{
return
this
.
set
(
key
.
getBytes
(),
value
,
activeTime
);
}
@Override
public
boolean
set
(
String
key
,
T
value
)
{
return
this
.
set
(
key
,
value
,
0L
);
}
@Override
public
String
get
(
String
key
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
String
>()
{
public
String
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
try
{
byte
[]
value
=
connection
.
get
(
key
.
getBytes
());
return
value
==
null
?
null
:
new
String
(
value
,
CHARSET
);
}
catch
(
UnsupportedEncodingException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
});
}
@Override
public
T
getObject
(
String
key
,
Class
<
T
>
c
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
T
>()
{
public
T
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
byte
[]
value
=
connection
.
get
(
key
.
getBytes
());
if
(
null
==
value
){
return
null
;
}
ObjectMapper
objectMapper
=
new
ObjectMapper
();
try
{
return
objectMapper
.
readValue
(
value
,
c
);
}
catch
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
});
}
@Override
public
Set
<
String
>
matchKeys
(
String
pattern
)
{
return
redisTemplate
.
keys
(
pattern
);
}
@Override
public
boolean
exists
(
String
key
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Boolean
>()
{
public
Boolean
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
exists
(
key
.
getBytes
());
}
});
}
@Override
public
boolean
flushDB
()
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Boolean
>()
{
public
Boolean
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
connection
.
flushDb
();
return
true
;
}
});
}
@Override
public
long
delete
(
Collection
<
String
>
keys
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Long
>()
{
public
Long
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
long
result
=
0
;
for
(
String
key
:
keys
)
{
result
+=
connection
.
del
(
key
.
getBytes
());
}
return
result
;
}
});
}
@Override
public
long
delete
(
String
...
keys
)
{
Collection
<
String
>
cols
=
new
ArrayList
<
String
>();
for
(
String
key
:
keys
)
{
cols
.
add
(
key
);
}
return
this
.
delete
(
cols
);
}
@Override
public
byte
[]
getBytes
(
String
key
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
byte
[]>()
{
public
byte
[]
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
get
(
key
.
getBytes
());
}
});
}
@Override
public
List
<
T
>
multiGet
(
Collection
keys
)
{
return
redisTemplate
.
opsForValue
().
multiGet
(
keys
);
}
public
class
RedisServiceImpl
implements
RedisService
{
@Resource
private
RedisTemplate
<
String
,
Serializable
>
redisTemplate
;
@Resource
private
StringRedisTemplate
stringRedisTemplate
;
@Override
public
void
set
(
String
key
,
String
value
)
{
stringRedisTemplate
.
opsForValue
().
set
(
key
,
value
);
}
@Override
public
void
set
(
String
key
,
String
value
,
long
seconds
)
{
stringRedisTemplate
.
opsForValue
().
set
(
key
,
value
,
seconds
,
TimeUnit
.
SECONDS
);
}
@Override
public
void
setIfAbsent
(
String
key
,
String
value
)
{
stringRedisTemplate
.
opsForValue
().
setIfAbsent
(
key
,
value
);
}
@Override
public
String
get
(
String
key
)
{
return
stringRedisTemplate
.
opsForValue
().
get
(
key
);
}
@Override
public
String
getAndSet
(
String
key
,
String
value
)
{
return
stringRedisTemplate
.
opsForValue
().
getAndSet
(
key
,
value
);
}
@Override
public
void
multiSet
(
Map
<
String
,
String
>
map
)
{
stringRedisTemplate
.
opsForValue
().
multiSet
(
map
);
}
@Override
public
List
<
String
>
multiGet
(
Collection
<
String
>
keys
)
{
return
stringRedisTemplate
.
opsForValue
().
multiGet
(
keys
);
}
@Override
public
Long
increment
(
String
key
,
long
delta
)
{
return
stringRedisTemplate
.
opsForValue
().
increment
(
key
,
delta
);
}
@Override
public
Double
increment
(
String
key
,
double
delta
)
{
return
stringRedisTemplate
.
opsForValue
().
increment
(
key
,
delta
);
}
@Override
public
Integer
append
(
String
key
,
String
value
)
{
return
stringRedisTemplate
.
opsForValue
().
append
(
key
,
value
);
}
@Override
public
String
get
(
String
key
,
long
start
,
long
end
)
{
return
stringRedisTemplate
.
opsForValue
().
get
(
key
,
start
,
end
);
}
@Override
public
Long
size
(
String
key
)
{
return
stringRedisTemplate
.
opsForValue
().
size
(
key
);
}
@Override
public
Boolean
setBit
(
String
key
,
long
offset
,
boolean
value
)
{
return
stringRedisTemplate
.
opsForValue
().
setBit
(
key
,
offset
,
value
);
}
@Override
public
Boolean
getBit
(
String
key
,
long
offset
)
{
return
stringRedisTemplate
.
opsForValue
().
getBit
(
key
,
offset
);
}
@Override
public
long
sadd
(
String
key
,
String
...
values
)
{
return
stringRedisTemplate
.
opsForSet
().
add
(
key
,
values
);
}
@Override
public
Set
<
String
>
smembers
(
String
key
)
{
return
stringRedisTemplate
.
opsForSet
().
members
(
key
);
}
@Override
public
void
convertAndSend
(
String
channel
,
Object
obj
)
{
assert
obj
!=
null
;
String
msg
=
obj
instanceof
String
?
String
.
valueOf
(
obj
)
:
JSON
.
toJSONString
(
obj
);
stringRedisTemplate
.
convertAndSend
(
channel
,
msg
);
}
@Override
public
void
delete
(
String
key
)
{
stringRedisTemplate
.
delete
(
key
);
}
@Override
public
boolean
exists
(
String
key
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Boolean
>()
{
@Override
public
Boolean
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
exists
(
key
.
getBytes
());
}
});
}
@Override
public
Set
<
String
>
matchKeys
(
String
pattern
)
{
return
redisTemplate
.
keys
(
pattern
);
}
@Override
public
<
T
>
void
set
(
String
key
,
T
value
,
long
seconds
)
throws
Exception
{
this
.
set
(
key
.
getBytes
(),
serialize
(
value
),
seconds
);
}
@Override
public
<
T
>
void
set
(
String
key
,
T
value
)
throws
Exception
{
this
.
set
(
key
.
getBytes
(),
serialize
(
value
));
}
@Override
public
<
T
>
T
get
(
String
key
,
Class
<
T
>
clazz
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
T
>()
{
@Override
public
T
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
byte
[]
value
=
connection
.
get
(
key
.
getBytes
());
if
(
value
==
null
)
{
return
null
;
}
ObjectMapper
objectMapper
=
new
ObjectMapper
();
try
{
return
objectMapper
.
readValue
(
value
,
clazz
);
}
catch
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
});
}
@Override
public
boolean
set
(
byte
[]
key
,
byte
[]
value
,
long
seconds
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Boolean
>()
{
@Override
public
Boolean
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
connection
.
setEx
(
key
,
seconds
,
value
);
return
true
;
}
});
}
@Override
public
boolean
set
(
byte
[]
key
,
byte
[]
value
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Boolean
>()
{
@Override
public
Boolean
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
connection
.
set
(
key
,
value
);
return
true
;
}
});
}
@Override
public
byte
[]
getBytes
(
String
key
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
byte
[]>()
{
@Override
public
byte
[]
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
get
(
key
.
getBytes
());
}
});
}
private
<
T
>
byte
[]
serialize
(
T
value
)
throws
Exception
{
ObjectMapper
objectMapper
=
new
ObjectMapper
();
return
objectMapper
.
writeValueAsBytes
(
value
);
}
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisSortedSetServiceImpl.java
已删除
100644 → 0
浏览文件 @
f337413c
package
org.hongxi.whatsmars.redis.client.service.impl
;
import
org.hongxi.whatsmars.redis.client.service.RedisSortedSetService
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.data.redis.connection.RedisConnection
;
import
org.springframework.data.redis.connection.RedisZSetCommands.Tuple
;
import
org.springframework.data.redis.core.RedisCallback
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
java.util.Set
;
@Service
public
class
RedisSortedSetServiceImpl
implements
RedisSortedSetService
{
@Resource
private
RedisTemplate
<
String
,
Set
<
Object
>>
redisTemplate
;
@Override
public
long
zadd
(
byte
[]
key
,
Set
<
Tuple
>
tuples
)
{
redisTemplate
.
execute
(
new
RedisCallback
<
Long
>()
{
@Override
public
Long
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
zAdd
(
key
,
tuples
);
}
});
return
0
;
}
@Override
public
Set
<
byte
[]>
zrange
(
byte
[]
key
,
int
start
,
int
end
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
Set
<
byte
[]>>()
{
@Override
public
Set
<
byte
[]>
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
zRange
(
key
,
start
,
end
);
}
});
}
@Override
public
long
zadd
(
String
key
,
Set
<
Tuple
>
tuples
)
{
return
this
.
zadd
(
key
.
getBytes
(),
tuples
);
}
@Override
public
Set
<
byte
[]>
zrange
(
String
key
)
{
return
this
.
zrange
(
key
,
0
,
-
1
);
}
@Override
public
Set
<
byte
[]>
zrange
(
String
key
,
int
start
,
int
end
)
{
return
this
.
zrange
(
key
.
getBytes
(),
start
,
end
);
}
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisStringServiceImpl.java
已删除
100644 → 0
浏览文件 @
f337413c
package
org.hongxi.whatsmars.redis.client.service.impl
;
import
com.alibaba.fastjson.JSON
;
import
org.hongxi.whatsmars.redis.client.service.RedisStringService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Service
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.TimeUnit
;
/**
* Created by shenhongxi on 2018/12/24.
*/
@Service
public
class
RedisStringServiceImpl
implements
RedisStringService
{
@Autowired
private
StringRedisTemplate
stringRedisTemplate
;
@Override
public
void
set
(
String
key
,
String
value
)
{
stringRedisTemplate
.
opsForValue
().
set
(
key
,
value
);
}
@Override
public
void
set
(
String
key
,
String
value
,
long
activeTime
)
{
stringRedisTemplate
.
opsForValue
().
set
(
key
,
value
,
activeTime
,
TimeUnit
.
SECONDS
);
}
@Override
public
void
setIfAbsent
(
String
key
,
String
value
)
{
stringRedisTemplate
.
opsForValue
().
setIfAbsent
(
key
,
value
);
}
@Override
public
String
get
(
String
key
)
{
return
stringRedisTemplate
.
opsForValue
().
get
(
key
);
}
@Override
public
String
getAndSet
(
String
key
,
String
value
)
{
return
stringRedisTemplate
.
opsForValue
().
getAndSet
(
key
,
value
);
}
@Override
public
void
multiSet
(
Map
<
String
,
String
>
map
)
{
stringRedisTemplate
.
opsForValue
().
multiSet
(
map
);
}
@Override
public
List
<
String
>
multiGet
(
Collection
<
String
>
keys
)
{
return
stringRedisTemplate
.
opsForValue
().
multiGet
(
keys
);
}
@Override
public
Long
increment
(
String
key
,
long
delta
)
{
return
stringRedisTemplate
.
opsForValue
().
increment
(
key
,
delta
);
}
@Override
public
Double
increment
(
String
key
,
double
delta
)
{
return
stringRedisTemplate
.
opsForValue
().
increment
(
key
,
delta
);
}
@Override
public
Integer
append
(
String
key
,
String
value
)
{
return
stringRedisTemplate
.
opsForValue
().
append
(
key
,
value
);
}
@Override
public
String
get
(
String
key
,
long
start
,
long
end
)
{
return
stringRedisTemplate
.
opsForValue
().
get
(
key
,
start
,
end
);
}
@Override
public
Long
size
(
String
key
)
{
return
stringRedisTemplate
.
opsForValue
().
size
(
key
);
}
@Override
public
Boolean
setBit
(
String
key
,
long
offset
,
boolean
value
)
{
return
stringRedisTemplate
.
opsForValue
().
setBit
(
key
,
offset
,
value
);
}
@Override
public
Boolean
getBit
(
String
key
,
long
offset
)
{
return
stringRedisTemplate
.
opsForValue
().
getBit
(
key
,
offset
);
}
@Override
public
long
sadd
(
String
key
,
String
...
values
)
{
return
stringRedisTemplate
.
opsForSet
().
add
(
key
,
values
);
}
@Override
public
Set
<
String
>
smembers
(
String
key
)
{
return
stringRedisTemplate
.
opsForSet
().
members
(
key
);
}
@Override
public
void
convertAndSend
(
String
channel
,
Object
obj
)
{
assert
obj
!=
null
;
String
msg
=
obj
instanceof
String
?
String
.
valueOf
(
obj
)
:
JSON
.
toJSONString
(
obj
);
stringRedisTemplate
.
convertAndSend
(
channel
,
msg
);
}
@Override
public
void
delete
(
String
key
)
{
stringRedisTemplate
.
delete
(
key
);
}
}
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/controller/InitRunner.java
浏览文件 @
3fb0e985
package
org.hongxi.whatsmars.spring.boot.controller
;
import
org.hongxi.whatsmars.redis.client.service.RedisS
tringS
ervice
;
import
org.hongxi.whatsmars.redis.client.service.RedisService
;
import
org.hongxi.whatsmars.spring.boot.async.MessageService
;
import
org.hongxi.whatsmars.spring.boot.dao.UserMapper
;
import
org.hongxi.whatsmars.spring.boot.model.User
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.CommandLineRunner
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Component
;
/**
...
...
@@ -24,7 +27,11 @@ public class InitRunner implements CommandLineRunner {
@Autowired
private
MessageService
messageService
;
@Autowired
private
RedisStringService
redisStringService
;
private
RedisService
redisService
;
@Autowired
private
RedisTemplate
redisTemplate
;
@Autowired
private
StringRedisTemplate
stringRedisTemplate
;
@Override
public
void
run
(
String
...
args
)
throws
Exception
{
...
...
@@ -36,7 +43,17 @@ public class InitRunner implements CommandLineRunner {
}
String
key
=
"domain"
;
redisStringService
.
set
(
key
,
"hongxi.org"
,
60
);
// 60s
System
.
out
.
println
(
redisStringService
.
get
(
key
));
redisService
.
set
(
key
,
"hongxi.org"
,
60
);
// 60s
System
.
out
.
println
(
redisService
.
get
(
key
));
User
user
=
new
User
();
user
.
setUsername
(
"javahongxi"
);
user
.
setNickname
(
"红喜"
);
redisService
.
set
(
"user"
,
user
);
System
.
out
.
println
(
redisService
.
get
(
"user"
));
System
.
out
.
println
(
redisService
.
get
(
"user"
,
User
.
class
));
redisService
.
sadd
(
"countries"
,
"China"
,
"America"
,
"Japan"
);
System
.
out
.
println
(
redisTemplate
.
opsForSet
().
isMember
(
"countries"
,
"China"
));
// fasle
System
.
out
.
println
(
stringRedisTemplate
.
opsForSet
().
isMember
(
"countries"
,
"China"
));
// true
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录