特别垃圾的redis 数据同步
来源:互联网 发布:男友那个很大 知乎 编辑:程序博客网 时间:2024/06/05 22:57
<!--整合redis的依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
<!--整合jedis的依赖包 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
util工具类
public class JedisUtil {
private static String HOST = "192.168.134.30";
private static int PORT = 6379;
private static JedisPool pool = null;
static {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(128);
config.setMaxIdle(80);
config.setMaxWaitMillis(2001);
pool = new JedisPool(config, HOST, PORT, 2000);
}
/**
* 把key存入redis中
*
* @param key k
* @param value v
* @param seconds 过期时间(秒)
* @return boolean
*/
public static boolean set(byte[] key, byte[] value, int seconds) {
Jedis jedis = null;
try {
jedis = pool.getResource();
String result = jedis.set(key, value);
if (seconds > 0) {
Long r = jedis.expire(key, seconds);
}
} catch (Exception e) {
return false;
} finally {
if (null != jedis) {
jedis.close();
}
}
return true;
}
public static byte[] get(byte[] key) {
byte[] value = null;
Jedis jedis = null;
try {
jedis = pool.getResource();
value = jedis.get(key);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return value;
}
/**
* 向缓存中设置对象
*
* @param key key
* @param obj value
* @return boolean
*/
public static boolean set(String key, Object obj, Integer seconds) {
Jedis jedis = null;
try {
jedis = pool.getResource();
ObjectMapper mapper = new ObjectMapper();
String value = mapper.writeValueAsString(obj);
jedis.set(SafeEncoder.encode(key), SafeEncoder.encode(value));
if (seconds != null) {
jedis.expire(key, seconds);
}
return true;
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return false;
}
/**
* 向缓存中设置对象
*
* @param key key
* @param value value
* @return boolean
*/
public static boolean set(String key, String value, Integer seconds) {
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.set(SafeEncoder.encode(key), SafeEncoder.encode(value));
if (seconds != null) {
jedis.expire(key, seconds);
}
return true;
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return false;
}
/**
* 移除缓存中设置对象
*
* @param keys 被删除的KEYS
* @return Long 被删除个数
*/
public static Long del(String... keys) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.del(keys);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
/**
* 根据key 获取对象
*
* @param key key
* @return T
*/
public static <T> T get(String key, Class<T> clazz) {
Jedis jedis = null;
try {
jedis = pool.getResource();
String v = jedis.get(key);
if (StringUtils.isNotEmpty(v)) {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(v, clazz);
}
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
/**
* 根据key值得到String类型的返回值
*
* @param key key
* @return String
*/
public static String get(String key) {
Jedis jedis = null;
try {
jedis = pool.getResource();
String v = jedis.get(key);
if (StringUtils.isEmpty(v)) {
return v;
}
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
public static Boolean exists(String key) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.exists(key);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
/**
* redis的list操作:
* 把元素插入到列表的尾部
*
* @param key KEY
* @param strings 要插入的值,变参
* @return 返回插入后list的大小
*/
public static Long rpush(String key, String... strings) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.rpush(key, strings);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
/**
* redis的list操作:
* 根据开始与结束下标取list中的值
*
* @param key KEY
* @param start 开始下标
* @param end 结束下标
* @return List<String>
*/
public static List<String> lrange(String key, int start, int end) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.lrange(key, start, end);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
/**
* redis的list操作:
* 取列表的长度
*
* @param key key
* @return Long
*/
public static Long llen(String key) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.llen(key);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
/**
* redis的list操作:
* 根据值移除list中的元素
*
* @param key KEY
* @param count :
* count > 0 : 从表头开始向表尾搜索,移除与 value 相等的元素,数量为 count 。
* count < 0 : 从表尾开始向表头搜索,移除与 value 相等的元素,数量为 count 的绝对值。
* count = 0 : 移除表中所有与 value 相等的值。
* @param value 要删除的值
* @return 返回被移除的个数
*/
public static Long lrem(String key, long count, String value) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.lrem(key, count, value);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
public static boolean setLong(String key, Long value) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return "OK".equals(jedis.set(key, String.valueOf(value)));
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return false;
}
public static Long getLong(String key) {
String result = get(key);
return result == null ? null : Long.valueOf(result);
}
public static Long incrBy(String key, Long increment) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.incrBy(key, increment);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
public static Long hashSet(String key, String field, String value) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hset(key, field, value);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return -1L;
}
public static Long hashSetLong(String key, String field, Long value) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hset(key, field, String.valueOf(value));
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return -1L;
}
public static Long hashIncrBy(String key, String field, Long increment) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hincrBy(key, field, increment);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return -1L;
}
public static Map<String, String> hashGetAll(String key) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hgetAll(key);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
public static Set<String> hashKeys(String key) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hkeys(key);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
public static Long hashDelAll(String key, String... fields) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.hdel(key, fields);
} catch (Exception e) {
} finally {
if (null != jedis) {
jedis.close();
}
}
return null;
}
}
在service 实现类中写两个方法
//判断redis数据库 是否有数据 没有则添加 相反则查询
public List<ShoppingCar> selectAll() {
String str = JedisUtil.get("ShoppingCarList");
if (str==null) {
List<ShoppingCar> list =shoppingCarMapper.selectAll();
String jsonString = JSON.toJSONString(list);
// jedis.set("userList", jsonString);
JedisUtil.set("ShoppingCarList", jsonString, 100000000);
return list;
}else{
List<ShoppingCar> list=(List<ShoppingCar>) JSON.parse(str);
return list;
}
}
//数据同步
public List<ShoppingCar> selectRedis() {
Jedis jedis = new Jedis("192.168.134.30", 6379);
List<ShoppingCar> list = shoppingCarMapper.selectAll();
String jsonString = JSON.toJSONString(list);
jedis.set("ShoppingCarList", jsonString);
return list;
}
- 特别垃圾的redis 数据同步
- redis数据同步
- Redis需特别注意的场景
- redis与mysql数据同步
- Redis 数据同步机制分析
- mysql到redis 同步数据
- redis与mysql数据同步
- redis与mysql数据同步
- redis与mysql数据同步
- redis与mysql数据同步
- redis与mysql数据同步
- Redis主从同步数据详解
- redis与mysql数据同步
- Redis 数据同步机制分析
- 通过Gearman实现MySQL到Redis的数据同步
- 通过Gearman实现MySQL到Redis的数据同步
- 通过Gearman实现MySQL到Redis的数据同步
- 通过Gearman实现MySQL到Redis的数据同步
- 206. Reverse Linked List。
- Android多进程应用
- (七)数据结构之搜索二叉树的简单实现
- 求两个数的最大公约数
- linux文件配色+vim配置
- 特别垃圾的redis 数据同步
- eclipse怎么集成配置JDK和Tomcat
- 深入浅出:快速掌握Java中UML类图及其关联关系的表示和应用
- python之pycharm如何安装第三方库
- CTF Web简单的SQL注入
- 图论day4(最小花费/刻录光盘)
- Python ::OS模块 提供的接口介绍
- Tablayout + viewpager
- 逆序的三位数