Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
武汉红喜
whatsmars
提交
05b028e4
W
whatsmars
项目概览
武汉红喜
/
whatsmars
通知
3
Star
0
Fork
1
代码
文件
提交
分支
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,体验更适合开发者的 AI 搜索 >>
提交
05b028e4
编写于
12月 30, 2018
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
RedisService封装简化
上级
3fb0e985
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
166 addition
and
18 deletion
+166
-18
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/RedisTemplate.java
...java/org/hongxi/whatsmars/redis/client/RedisTemplate.java
+81
-0
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisService.java
...g/hongxi/whatsmars/redis/client/service/RedisService.java
+17
-0
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisServiceImpl.java
...whatsmars/redis/client/service/impl/RedisServiceImpl.java
+57
-17
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/controller/InitRunner.java
...g/hongxi/whatsmars/spring/boot/controller/InitRunner.java
+8
-0
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/model/User.java
...ain/java/org/hongxi/whatsmars/spring/boot/model/User.java
+3
-1
未找到文件。
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/RedisTemplate.java
浏览文件 @
05b028e4
package
org.hongxi.whatsmars.redis.client
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
lombok.extern.slf4j.Slf4j
;
import
redis.clients.jedis.ShardedJedis
;
import
redis.clients.jedis.ShardedJedisPool
;
...
...
@@ -10,10 +12,13 @@ import java.util.*;
* 单个redis模式作为sharding模式的特例(配一个host:port即可)
* JedisCluster不用封装,直接用即可
*/
@Slf4j
public
class
RedisTemplate
{
private
ShardedJedisPool
shardedJedisPool
;
private
ObjectMapper
objectMapper
=
new
ObjectMapper
();
public
<
T
>
T
execute
(
RedisCallback
<
T
>
action
)
{
ShardedJedis
jedis
=
fetchJedis
();
try
{
...
...
@@ -155,6 +160,15 @@ public class RedisTemplate {
}
}
public
boolean
sismember
(
String
key
,
String
value
)
{
ShardedJedis
jedis
=
fetchJedis
();
try
{
return
jedis
.
sismember
(
key
,
value
);
}
finally
{
release
(
jedis
);
}
}
public
long
lpush
(
String
key
,
String
...
values
)
{
ShardedJedis
jedis
=
fetchJedis
();
try
{
...
...
@@ -200,6 +214,56 @@ public class RedisTemplate {
}
}
public
boolean
hexists
(
String
key
,
String
field
)
{
ShardedJedis
jedis
=
fetchJedis
();
try
{
return
jedis
.
hexists
(
key
,
field
);
}
finally
{
release
(
jedis
);
}
}
public
long
hdel
(
String
key
,
String
...
fields
)
{
ShardedJedis
jedis
=
fetchJedis
();
try
{
return
jedis
.
hdel
(
key
,
fields
);
}
finally
{
release
(
jedis
);
}
}
public
<
T
>
void
set
(
String
key
,
T
value
,
int
seconds
)
throws
Exception
{
ShardedJedis
jedis
=
fetchJedis
();
try
{
jedis
.
setex
(
key
.
getBytes
(),
seconds
,
this
.
serialize
(
value
));
}
finally
{
release
(
jedis
);
}
}
public
<
T
>
void
set
(
String
key
,
T
value
)
throws
Exception
{
ShardedJedis
jedis
=
fetchJedis
();
try
{
jedis
.
set
(
key
.
getBytes
(),
this
.
serialize
(
value
));
}
finally
{
release
(
jedis
);
}
}
public
<
T
>
T
get
(
String
key
,
Class
<
T
>
clazz
)
{
byte
[]
value
=
this
.
getBytes
(
key
);
return
this
.
deserialize
(
value
,
clazz
);
}
public
byte
[]
getBytes
(
String
key
)
{
ShardedJedis
jedis
=
fetchJedis
();
try
{
return
jedis
.
get
(
key
.
getBytes
());
}
finally
{
release
(
jedis
);
}
}
public
void
setShardedJedisPool
(
ShardedJedisPool
shardedJedisPool
)
{
this
.
shardedJedisPool
=
shardedJedisPool
;
}
...
...
@@ -213,4 +277,21 @@ public class RedisTemplate {
jedis
.
close
();
}
}
public
void
setObjectMapper
(
ObjectMapper
objectMapper
)
{
this
.
objectMapper
=
objectMapper
;
}
private
<
T
>
byte
[]
serialize
(
T
value
)
throws
Exception
{
return
objectMapper
.
writeValueAsBytes
(
value
);
}
private
<
T
>
T
deserialize
(
byte
[]
value
,
Class
<
T
>
clazz
)
{
try
{
return
objectMapper
.
readValue
(
value
,
clazz
);
}
catch
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisService.java
浏览文件 @
05b028e4
package
org.hongxi.whatsmars.redis.client.service
;
import
java.io.Serializable
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -42,6 +43,22 @@ public interface RedisService {
Set
<
String
>
smembers
(
String
key
);
boolean
sismember
(
String
key
,
String
value
);
void
hset
(
String
key
,
String
hashKey
,
String
value
);
String
hget
(
String
key
,
String
hashKey
);
boolean
hexists
(
String
key
,
String
hashKey
);
void
hdel
(
String
key
,
String
...
hashKeys
);
long
leftPush
(
String
key
,
Serializable
value
);
Serializable
rightPop
(
String
key
);
List
<
Serializable
>
range
(
String
key
,
long
start
,
long
end
);
void
convertAndSend
(
String
channel
,
Object
obj
);
void
delete
(
String
key
);
...
...
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisServiceImpl.java
浏览文件 @
05b028e4
...
...
@@ -2,6 +2,7 @@ package org.hongxi.whatsmars.redis.client.service.impl;
import
com.alibaba.fastjson.JSON
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
lombok.Setter
;
import
lombok.extern.slf4j.Slf4j
;
import
org.hongxi.whatsmars.redis.client.service.RedisService
;
import
org.springframework.dao.DataAccessException
;
...
...
@@ -30,6 +31,8 @@ public class RedisServiceImpl implements RedisService {
private
RedisTemplate
<
String
,
Serializable
>
redisTemplate
;
@Resource
private
StringRedisTemplate
stringRedisTemplate
;
@Setter
private
ObjectMapper
objectMapper
=
new
ObjectMapper
();
@Override
public
void
set
(
String
key
,
String
value
)
{
...
...
@@ -111,6 +114,49 @@ public class RedisServiceImpl implements RedisService {
return
stringRedisTemplate
.
opsForSet
().
members
(
key
);
}
@Override
public
boolean
sismember
(
String
key
,
String
value
)
{
return
stringRedisTemplate
.
opsForSet
().
isMember
(
key
,
value
);
}
@Override
public
void
hset
(
String
key
,
String
hashKey
,
String
value
)
{
redisTemplate
.
opsForHash
().
put
(
key
,
hashKey
,
value
);
}
@Override
public
String
hget
(
String
key
,
String
hashKey
)
{
if
(
hexists
(
key
,
hashKey
))
{
return
redisTemplate
.
opsForHash
().
get
(
key
,
hashKey
).
toString
();
}
return
null
;
}
@Override
public
boolean
hexists
(
String
key
,
String
hashKey
)
{
return
redisTemplate
.
opsForHash
().
hasKey
(
key
,
hashKey
);
}
@Override
public
void
hdel
(
String
key
,
String
...
hashKeys
)
{
redisTemplate
.
opsForHash
().
delete
(
key
,
hashKeys
);
}
@Override
public
long
leftPush
(
String
key
,
Serializable
value
)
{
return
redisTemplate
.
opsForList
().
leftPush
(
key
,
value
);
}
@Override
public
Serializable
rightPop
(
String
key
)
{
return
redisTemplate
.
opsForList
().
rightPop
(
key
);
}
@Override
public
List
<
Serializable
>
range
(
String
key
,
long
start
,
long
end
)
{
return
redisTemplate
.
opsForList
().
range
(
key
,
start
,
end
);
}
@Override
public
void
convertAndSend
(
String
channel
,
Object
obj
)
{
assert
obj
!=
null
;
...
...
@@ -150,22 +196,8 @@ public class RedisServiceImpl implements RedisService {
@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
;
}
});
byte
[]
value
=
this
.
getBytes
(
key
);
return
this
.
deserialize
(
value
,
clazz
);
}
@Override
...
...
@@ -201,8 +233,16 @@ public class RedisServiceImpl implements RedisService {
}
private
<
T
>
byte
[]
serialize
(
T
value
)
throws
Exception
{
ObjectMapper
objectMapper
=
new
ObjectMapper
();
return
objectMapper
.
writeValueAsBytes
(
value
);
}
private
<
T
>
T
deserialize
(
byte
[]
value
,
Class
<
T
>
clazz
)
{
try
{
return
objectMapper
.
readValue
(
value
,
clazz
);
}
catch
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
}
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/controller/InitRunner.java
浏览文件 @
05b028e4
...
...
@@ -55,5 +55,13 @@ public class InitRunner implements CommandLineRunner {
redisService
.
sadd
(
"countries"
,
"China"
,
"America"
,
"Japan"
);
System
.
out
.
println
(
redisTemplate
.
opsForSet
().
isMember
(
"countries"
,
"China"
));
// fasle
System
.
out
.
println
(
stringRedisTemplate
.
opsForSet
().
isMember
(
"countries"
,
"China"
));
// true
redisService
.
hset
(
"menu"
,
"A"
,
"a"
);
System
.
out
.
println
(
redisService
.
hget
(
"menu"
,
"A"
));
redisService
.
leftPush
(
"message"
,
"haha"
);
redisService
.
leftPush
(
"message"
,
user
);
System
.
out
.
println
(
redisService
.
rightPop
(
"message"
));
System
.
out
.
println
(
redisService
.
rightPop
(
"message"
));
}
}
whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/model/User.java
浏览文件 @
05b028e4
...
...
@@ -2,12 +2,14 @@ package org.hongxi.whatsmars.spring.boot.model;
import
com.fasterxml.jackson.annotation.JsonProperty
;
import
java.io.Serializable
;
import
java.util.Date
;
/**
* Created by shenhongxi on 2017/6/26.
*/
public
class
User
{
public
class
User
implements
Serializable
{
private
static
final
long
serialVersionUID
=
6469790070035101683L
;
@JsonProperty
(
"user_id"
)
// 有时候前端定义的字段名与后端不一致时采用此法处理
private
Long
id
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录