报错案例:ERR wrong number of arguments for ‘sadd’ command
这是因为在查询所有的key值时,得到的数组是空的
jedis.sadd(“fangchuantou_notice”,arr);// arr 放着所有key的数组
步骤一:在application.yml中添加redis信息
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///ggt
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
redis:
host: 10.9.251.200
port: 8100
password: redis001
jedis:
pool:
max-active: 1024
max-idle: 100
min-idle: 10
pagehelper:
helper-dialect: mysql
params: count=countsql
reasonable: true
support-methods-arguments: true
步骤二::添加配置文件类,在服务器启动的时候创建JedisPool对象
package com.qf.fayuan.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfig {
@Value(value = ("${spring.redis.host}"))
private String host;
@Value(value = ("${spring.redis.port}"))
private int port;
@Value(value =("${spring.redis.password}") )
private String password;
@Bean
public JedisPool jedisPool(JedisPoolConfig jedisPoolConfig){
JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,5000,password);
return jedisPool;
}
@Bean
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(1024);
jedisPoolConfig.setMaxIdle(100);
jedisPoolConfig.setMinIdle(10);
return jedisPoolConfig;
}
}
步骤三:配置工具类,如果缓存中没有,将数据添加入
package com.qf.fayuan.utils;
import redis.clients.jedis.Jedis;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class RedisUtils {
public static void hSet(int key, Object object, Jedis jedis){
//获取类的字节码对象
Class> aClass = object.getClass();
//获得类中的所有属性
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field : declaredFields) {
String fieldName = field.getName();
try {
//获取属性的get/set 方法
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(fieldName, aClass);
//获取get方法
Method readMethod = propertyDescriptor.getReadMethod();
if (readMethod != null) {
//得到属性的值
Object result = readMethod.invoke(object);
if (result != null) {
//向缓存中添加数据
jedis.hset(key + "noticeinfo", fieldName, result.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
第四部分:service部分
/**
* 通过查询id获得公告的信息,这里使用缓存来提高效率
* @param id 公告信息的id
* @return
*/
@Override
public ResultBean getNoticeInfo(int id) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//从缓存中读取,判断缓存中是否含有数据
Map stringMap = jedis.hgetAll(id + "noticeinfo");
if (stringMap == null || stringMap.isEmpty()) {
//设置防止穿透,判断数据库中是否含有该项数据,没有的话就不用去查询数据库了
Boolean fangchuantou_notice = jedis.sismember("fangchuantou_notice", String.valueOf(id));
if (fangchuantou_notice) {
Notice notice = noticeMapper.getNoticeInfo(id);
if (notice != null) {
//将数据添加到缓存中
RedisUtils.hSet(id, notice, jedis);
//设置数据在缓存中的保存时间,单位是 秒
jedis.expire(id + "noticeinfo", 1800);
return ResultBean.setOk(notice);
}
}
}else {
return ResultBean.setOk(stringMap);
}
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
if (jedis != null) {
jedis.close();
}
}
return ResultBean.setError(ErrorCodeInterface.XIUGAIMIMASHIBAI,"查询失败",null);
}
第五部分:为了防止穿透,在服务器启动的时候就遍历数据库,将所有的key值便利出来
package com.qf.fayuan.config;
import com.qf.fayuan.mapper.NoticeMapper;
import com.qf.fayuan.notice.pojo.Notice;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import javax.annotation.PostConstruct;
import java.util.List;
@Configuration
public class GetAllRedisConfig {
@Autowired
private JedisPool jedisPool;
@Autowired
private NoticeMapper noticeMapper;
@PostConstruct
public void init(){
Jedis jedis = jedisPool.getResource();
List list = noticeMapper.getAllNoticeKey();
// String[] arr = list.toArray(new String[list.size()]);
String[] arr = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
arr[i] = list.get(i).toString();
}
jedis.sadd("fangchuantou_notice",arr);
jedis.close();
}
}
第六部分:在用户执行其他操作时(增删改查),要实时更新防穿透列表
@Service
public class NoticeServiceImpl implements NoticeService {
@Autowired
private NoticeMapper noticeMapper;
@Autowired
private JedisPool jedisPool;
@Override
public void addNotice(Notice notice) {
noticeMapper.addNotice(notice);
//比如添加数据的时候,更新放穿透列表
Jedis jedis = jedisPool.getResource();
jedis.sadd("fangchuantou_notice",notice.getId()+"");
jedis.close();
}
上面注意的是,添加数据后如何获取对象的id
要将selectkey 放在sql插入语句后面
insert into rw_notice(number, user_id, notice_type_id, court_id, case_num,show_area, about_mobile, about_identify_num, about_name, name_id, create_time, update_time, img,status, trade_no, pub_time, pub_days) values (#{number},#{user_id},#{notice_type_id},#{court_id},#{case_num},#{show_area},#{about_mobile},#{about_identify_num},#{about_name},#{name_id},#{create_time},#{update_time},#{img},#{status},#{trade_no},#{pub_time},#{pub_days})
select LAST_INSERT_ID() as value