隐藏页面特效

jedis的使用

1|0Jedis基本使用


  • 导入依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.7.1</version> </dependency>
  • 案例
public class RedisTest { private Jedis jedis; @Before public void setJedis(){ //建立连接 jedis = new Jedis("127.0.0.1",6379); //设置密码,没有不写 //jedis.auth() //选择库,默认是0 jedis.select(0); } @After public void destroyJedis(){ if (jedis!=null) jedis.close(); } @Test public void fn(){ String result=jedis.set("name","hello"); System.out.println("name-->"+result);//ok String name = jedis.get("name"); System.out.println(name);//hello } }

2|0Jedis连接池


  • Jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,因此应该使用线程池代替Jedis的直连方式
//工具类的封装 public class JedisConnPool { private static JedisPool jedisPool; static { JedisPoolConfig jedisPoolConfig=new JedisPoolConfig(); //最大连接数 jedisPoolConfig.setMaxTotal(10); //最大空闲连接 jedisPoolConfig.setMaxIdle(10); //最小空闲连接 jedisPoolConfig.setMinIdle(0); //设置最长等待时间 jedisPoolConfig.setMaxWaitMillis(200); jedisPool = new JedisPool(jedisPoolConfig,"127.0.0.1",6379,1000,null); } public static Jedis getJedis(){ return jedisPool.getResource(); } }

3|0SpringDataRedis


  • 导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency>
  • yml配置信息
spring: main: banner-mode: off redis: host: 127.0.0.1 port: 6379 #password: lettuce: pool: max-active: 10 # 最大连接 max-idle: 10 # 最大空闲连接 min-idle: 0 # 最小空闲连接 max-wait: 100 # 连接等待时间
  • 编写测试
@SpringBootTest public class RedisTest { @Autowired private RedisTemplate redisTemplate; @Test public void fun(){ redisTemplate.opsForValue().set("name", "xue"); Object name = redisTemplate.opsForValue().get("name"); System.out.println(name); } }
  • 配置RedisTemplate的序列化,这样会自动将对象序列化成json字符串,但是这样的话会占用大量的内存空间,因为自动序列化时,会多一个属性-@class
@Configuration public class RedisConfig { @Bean public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){ //创建RedisTemplate对象 RedisTemplate<String,Object> template = new RedisTemplate<>(); //设置连接工厂 template.setConnectionFactory(connectionFactory); //设置JSON序列化工具 GenericJackson2JsonRedisSerializer JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); //设置Key的序列化 template.setKeySerializer(RedisSerializer.string()); template.setHashKeySerializer(RedisSerializer.string()); //设置value的序列化 template.setHashValueSerializer(JsonRedisSerializer); template.setValueSerializer(JsonRedisSerializer); return template; } } /*{ "@class": "com.example.springdatademo.bean.User", //多余的属性 "username": "jack", "age": 22, "sex": "男" }*/
  • 使用StringRedisTemplate手动序列化和反序列化
@SpringBootTest public class StringRedisTest { @Autowired private StringRedisTemplate stringRedisTemplate; private final ObjectMapper objConvertStr = new ObjectMapper(); @Test public void fun() throws JsonProcessingException { //创建对象 User user = new User("test",22,"男"); //手动转化为json字符串 String json = objConvertStr.writeValueAsString(user); //写入数据 stringRedisTemplate.opsForValue().set("user:2",json); //读取数据 String str = stringRedisTemplate.opsForValue().get("user:2"); //反序列化 User obj = objConvertStr.readValue(str, User.class); System.out.println(obj); } } /*{ "username": "jack", "age": 22, "sex": "男" }*/

__EOF__

本文作者keep on going,never give up
本文链接https://www.cnblogs.com/-xyk/p/17636338.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   彼时听风  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示