redis缓存的数据,必须序列化成字符串或者字节数组 package com.example.Interceptor;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.config.MyConfiguration;
import com.example.domain.Emp;
import com.example.util.CacheKeyGenerator;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.*;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;@Intercepts({@Signature(type = Executor.class,method = "query",args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
)})
public class CacheInterceptor implements Interceptor {@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {}public static byte[] serialize(Object obj) throws IOException {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);objectOutputStream.writeObject(obj);return byteArrayOutputStream.toByteArray();}private static JedisPool pool;static {System.out.println("static....");// 配置连接池JedisPoolConfig poolConfig = new JedisPoolConfig();poolConfig.setMaxTotal(30); // 最大连接数poolConfig.setMaxIdle(10); // 最大空闲连接数poolConfig.setMinIdle(5); // 最小空闲连接数poolConfig.setTestOnBorrow(true); // 借用连接时进行连接测试poolConfig.setTestOnReturn(true); // 归还连接时进行连接测试poolConfig.setTestWhileIdle(true); // 空闲时进行连接测试poolConfig.setMinEvictableIdleTimeMillis(60000); // 逐出连接的最小空闲时间poolConfig.setTimeBetweenEvictionRunsMillis(30000); // 逐出扫描的时间间隔// 创建连接池pool = new JedisPool(poolConfig, "localhost", 6379);}@Overridepublic Object intercept(Invocation invocation) throws Throwable {System.out.println("intercept....");Map<?, ?> arg = ( Map<?, ?>) invocation.getArgs()[1];System.out.println(arg);Map<String, Object> paramNameValuePairs=null;Long id=null;for (Object entry : arg.values()) {if(entry!=null) {QueryWrapper<?> queryWrapper_emp = (QueryWrapper<?>) entry;System.out.println(queryWrapper_emp.getCustomSqlSegment());paramNameValuePairs = queryWrapper_emp.getParamNameValuePairs();System.out.println(paramNameValuePairs);id = (Long) paramNameValuePairs.get("MPGENVAL1");System.out.println(id);if(!MyConfiguration.bloomFilter.mightContain(Math.toIntExact(id))){System.out.println("不存在");ArrayList<Emp> objects = new ArrayList<>();objects.add(new Emp());return objects;}System.out.println("paramNameValuePairs=" + paramNameValuePairs);}break;}String generateKey = CacheKeyGenerator.generateKey(paramNameValuePairs);Jedis jedis=null;try {jedis= pool.getResource();byte[] generateKeybytes = jedis.get(generateKey.getBytes());if(generateKeybytes!=null){Object deserialize = deserialize(generateKeybytes);System.out.println("命中");return deserialize;}else{System.out.println("未命中");Object proceed = invocation.proceed();jedis.set(generateKey.getBytes(),serialize(proceed));return proceed;}}catch (Exception e){e.printStackTrace();}finally {jedis.close();}return null;}// 反序列化对象public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);return objectInputStream.readObject();}
}
package com.example.config;import com.example.domain.Emp;
import com.example.mapper.EmpMapper;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;import java.util.Iterator;
import java.util.List;@Configuration
public class MyConfiguration {public static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), 10000, 0.01);@Autowiredpublic void setEmpMapper(EmpMapper empMapper){List<Emp> emps = empMapper.selectList(null);Iterator<Emp> iterator = emps.iterator();while (iterator.hasNext()){Emp next = iterator.next();bloomFilter.put(Math.toIntExact(next.getId()));}System.out.println(bloomFilter.mightContain(3214044));System.out.println(bloomFilter.mightContain(7213478));}
}