Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
武汉红喜
whatsmars
提交
d4d82ee1
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,发现更多精彩内容 >>
提交
d4d82ee1
编写于
12月 24, 2018
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
optimise
上级
4dcf9a4e
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
178 addition
and
45 deletion
+178
-45
whatsmars-redis/README.md
whatsmars-redis/README.md
+4
-3
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisService.java
...g/hongxi/whatsmars/redis/client/service/RedisService.java
+8
-7
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisStringService.java
...xi/whatsmars/redis/client/service/RedisStringService.java
+46
-0
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisMapServiceImpl.java
...tsmars/redis/client/service/impl/RedisMapServiceImpl.java
+3
-1
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisServiceImpl.java
...whatsmars/redis/client/service/impl/RedisServiceImpl.java
+7
-34
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisStringServiceImpl.java
...ars/redis/client/service/impl/RedisStringServiceImpl.java
+110
-0
未找到文件。
whatsmars-redis/README.md
浏览文件 @
d4d82ee1
...
...
@@ -13,11 +13,11 @@ Spring Boot 2.x默认使用Lettuce。
-
Jedis相比Lettuce和
[
Redission
](
https://github.com/redisson/redisson
)
,最大的特点是简单易集成,这从源代码量就可感受到。
### Redis集群方案
-
客户端分片: 操作简单,无法动态扩缩容
`
现实
方案`
-
[
Twemproxy
](
https://github.com/twitter/twemproxy
)
/
[
Codis
](
https://github.com/CodisLabs/codis
)
:
`多余
方案`
-
客户端分片: 操作简单,无法动态扩缩容
`
简单
方案`
-
[
Twemproxy
](
https://github.com/twitter/twemproxy
)
/
[
Codis
](
https://github.com/CodisLabs/codis
)
:
云厂商大都采用这类方案
`代理
方案`
-
Redis Cluster: 官方集群方案,无中心结构,动态扩缩容,最大支撑1000个节点,
`理想方案`
###
主从读写分离
###
Master/Slave Separation
通常情况下,Slave只是作为数据备份,不提供read操作,这种考虑是为了避免slave提供stale数据而导致一些问题。
不过在很多场景下,即使slave数据有一定的延迟,我们仍然可以兼容或者正常处理,此时我们可以将slave提供read
服务,并在M-S集群中将read操作分流,此时我们的Redis集群将可以支撑更高的QPS。本实例中,仅仅提供了“读写分
...
...
@@ -38,4 +38,5 @@ MySQL的主要任务是把数据组织成树表,在磁盘和内存之间进行
-
[
如何根据key前缀统计内存占用
](
https://segmentfault.com/q/1010000010575235
)
-
[
《Redis设计与实现》
](
https://e.jd.com/30189715.html
)
`e.jd.com`
[
whatsmars-spring-boot
](
https://github.com/javahongxi/whatsmars/tree/master/whatsmars-spring-boot
)
[
whatsmars-boot-sample-redis
](
https://github.com/javahongxi/whatsmars/tree/master/whatsmars-spring-boot-samples/whatsmars-boot-sample-redis
)
\ No newline at end of file
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisService.java
浏览文件 @
d4d82ee1
...
...
@@ -80,7 +80,7 @@ public interface RedisService<T> {
T
getObject
(
String
key
,
Class
<
T
>
c
);
byte
[]
getB
(
String
key
);
byte
[]
getB
ytes
(
String
key
);
/**
* <pre>
...
...
@@ -108,11 +108,12 @@ public interface RedisService<T> {
*/
boolean
flushDB
();
void
pubMsg
(
String
channel
,
Object
obj
);
Set
<
String
>
getSet
(
String
key
);
long
addSet
(
String
key
,
String
...
values
);
/**
* <pre>
*
* @param keys
* @return 批量获取key的值
* </pre>
*/
List
<
T
>
multiGet
(
Collection
keys
);
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/RedisStringService.java
0 → 100644
浏览文件 @
d4d82ee1
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
addSet
(
String
key
,
String
...
values
);
Set
<
String
>
getSet
(
String
key
);
void
pubMsg
(
String
channel
,
Object
obj
);
}
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisMapServiceImpl.java
浏览文件 @
d4d82ee1
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
;
...
...
@@ -14,6 +15,7 @@ import java.util.List;
import
java.util.Map
;
import
java.util.Set
;
@Slf4j
@Service
public
class
RedisMapServiceImpl
implements
RedisMapService
{
...
...
@@ -121,7 +123,7 @@ public class RedisMapServiceImpl implements RedisMapService {
try
{
return
value
==
null
?
null
:
new
String
(
value
,
CHARSET
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
(
);
log
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
...
...
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisServiceImpl.java
浏览文件 @
d4d82ee1
package
org.hongxi.whatsmars.redis.client.service.impl
;
import
com.alibaba.fastjson.JSON
;
import
com.fasterxml.jackson.core.JsonParseException
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.JsonMappingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
lombok.extern.slf4j.Slf4j
;
import
org.hongxi.whatsmars.redis.client.service.RedisService
;
import
org.springframework.beans.factory.annotation.Autowired
;
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.IOException
;
import
java.io.Serializable
;
import
java.io.UnsupportedEncodingException
;
import
java.util.ArrayList
;
...
...
@@ -23,6 +18,7 @@ import java.util.Collection;
import
java.util.List
;
import
java.util.Set
;
@Slf4j
@Service
public
class
RedisServiceImpl
<
T
>
implements
RedisService
<
T
>
{
...
...
@@ -30,8 +26,6 @@ public class RedisServiceImpl<T> implements RedisService<T> {
@Resource
private
RedisTemplate
<
String
,
Serializable
>
redisTemplate
;
@Autowired
private
StringRedisTemplate
stringRedisTemplate
;
@Override
public
boolean
set
(
final
byte
[]
key
,
final
byte
[]
value
,
final
long
activeTime
)
{
...
...
@@ -69,7 +63,7 @@ public class RedisServiceImpl<T> implements RedisService<T> {
try
{
b
=
objectMapper
.
writeValueAsBytes
(
value
);
}
catch
(
JsonProcessingException
e
)
{
e
.
printStackTrace
(
);
log
.
error
(
e
.
getMessage
(),
e
);
return
false
;
}
return
this
.
set
(
key
,
b
,
activeTime
);
...
...
@@ -83,7 +77,7 @@ public class RedisServiceImpl<T> implements RedisService<T> {
byte
[]
value
=
connection
.
get
(
key
.
getBytes
());
return
value
==
null
?
null
:
new
String
(
value
,
CHARSET
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
(
);
log
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
...
...
@@ -101,12 +95,8 @@ public class RedisServiceImpl<T> implements RedisService<T> {
ObjectMapper
objectMapper
=
new
ObjectMapper
();
try
{
return
objectMapper
.
readValue
(
value
,
c
);
}
catch
(
JsonParseException
e
)
{
e
.
printStackTrace
();
}
catch
(
JsonMappingException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
...
...
@@ -160,7 +150,7 @@ public class RedisServiceImpl<T> implements RedisService<T> {
}
@Override
public
byte
[]
getB
(
final
String
key
)
{
public
byte
[]
getB
ytes
(
final
String
key
)
{
return
redisTemplate
.
execute
(
new
RedisCallback
<
byte
[]>()
{
public
byte
[]
doInRedis
(
RedisConnection
connection
)
throws
DataAccessException
{
return
connection
.
get
(
key
.
getBytes
());
...
...
@@ -168,23 +158,6 @@ public class RedisServiceImpl<T> implements RedisService<T> {
});
}
@Override
public
void
pubMsg
(
String
channel
,
Object
obj
)
{
assert
null
!=
obj
;
String
msg
=
obj
instanceof
String
?
String
.
valueOf
(
obj
)
:
JSON
.
toJSONString
(
obj
);
stringRedisTemplate
.
convertAndSend
(
channel
,
msg
);
}
@Override
public
Set
<
String
>
getSet
(
String
key
)
{
return
stringRedisTemplate
.
opsForSet
().
members
(
key
);
}
@Override
public
long
addSet
(
String
key
,
String
...
values
)
{
return
stringRedisTemplate
.
opsForSet
().
add
(
key
,
values
);
}
@Override
public
List
<
T
>
multiGet
(
Collection
keys
)
{
return
redisTemplate
.
opsForValue
().
multiGet
(
keys
);
...
...
whatsmars-redis/src/main/java/org/hongxi/whatsmars/redis/client/service/impl/RedisStringServiceImpl.java
0 → 100644
浏览文件 @
d4d82ee1
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
addSet
(
String
key
,
String
...
values
)
{
return
stringRedisTemplate
.
opsForSet
().
add
(
key
,
values
);
}
@Override
public
Set
<
String
>
getSet
(
String
key
)
{
return
stringRedisTemplate
.
opsForSet
().
members
(
key
);
}
@Override
public
void
pubMsg
(
String
channel
,
Object
obj
)
{
assert
obj
!=
null
;
String
msg
=
obj
instanceof
String
?
String
.
valueOf
(
obj
)
:
JSON
.
toJSONString
(
obj
);
stringRedisTemplate
.
convertAndSend
(
channel
,
msg
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录