提交 d4d82ee1 编写于 作者: 武汉红喜's avatar 武汉红喜

optimise

上级 4dcf9a4e
......@@ -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
......@@ -80,7 +80,7 @@ public interface RedisService<T> {
T getObject(String key, Class<T> c);
byte[] getB(String key);
byte[] getBytes(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>
*
* &#64;param keys
* &#64;return 批量获取key的值
* </pre>
*/
List<T> multiGet(Collection keys);
}
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);
}
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;
}
......
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[] getBytes(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);
......
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.
先完成此消息的编辑!
想要评论请 注册