栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

redis学习笔记(十七)redis+springboot API使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

redis学习笔记(十七)redis+springboot API使用

一、API介绍

API是使用spring data redis
可以选择拿同步连接和异步连接
可以选择优先从从库里读数据
redisTemplate是高级API
jedisConnection是低级API
高级API依赖于低级API

二、maven

    org.springframework.boot
    spring-boot-starter-data-redis


    org.springframework.boot
    spring-boot-starter-json

三、使用 1.使用低级API:
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
connection.set("hello".getBytes(), "yangzhciheng".getBytes());
2.json序列化
Jackson2HashMapper jackson2HashMapper = new Jackson2HashMapper(objectMapper, false);
redisTemplate.opsForHash().putAll("sean", jackson2HashMapper.toHash(new Object()));
Map sean = redisTemplate.opsForHash().entries("sean");
Object o = objectMapper.convertValue(sean, Object.class);
3.自定义template
@Bean
public StringRedisTemplate myTemplate(RedisConnectionFactory redisConnectionFactory) {
    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory);
    stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer(Object.class));
    return stringRedisTemplate;
}
 
4.pub/sub的API使用 
stringRedisTemplate.convertAndSend("ooxx", "hello");
RedisConnection connection = stringRedisTemplate.getConnectionFactory().getConnection();
connection.subscribe(new MessageListener() {
    @Override
    public void onMessage(Message message, byte[] pattern) {
        System.out.println(message);
        // TODO
    }
}, "ooxx".getBytes());

如果有写错的地方,欢迎大家指正,感谢!

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号