• 在SpringBoot⼯程连接Redis


    Spring Data Redis依赖中,提供了⽤于连接redis的客户端:

    RedisTemplate

    StringRedisTemplate

    创建springBoot应⽤

    或者可以在现有的SpringBoot项目的Maven中引入

        org.springframework.boot

        spring-boot-starter-data-redis

    配置redis

    application.yml⽂件配置redis的连接信息

    spring:

        redis:

            host: 47.96.11.185

            port: 6379

            database: 0

            password: 123456

    使⽤redis客户端连接redis

    直接在service中注⼊ RedisTemplate 或者 StringRedisTemplate ,就可以使⽤此对象完成redis操作

    @Service

    public class ProductServiceImpl implements ProductService {

        @Autowired

        private StringRedisTemplate stringRedisTemplate;

        public void addInfoToRedis() {

            try {

                Product product =new Product("103","王老吉",4.0);

                String s =new ObjectMapper().writeValueAsString(product);

                stringRedisTemplate.boundValueOps(product.getProductId()).set(s);

            }catch (JsonProcessingException e) {

                e.printStackTrace();

            }

        }

        public Product getInfoFromRedis(String productId) {

            Product product =null;

            try {

                String s =stringRedisTemplate.boundValueOps(productId).get();

                product =new ObjectMapper().readValue(s,Product.class);

            }catch (JsonProcessingException e) {

                e.printStackTrace();

            }

            return product;

        }

    }

  • 相关阅读:
    使用 Typescript 封装 Axios
    MHA+MYCAT 高可用架构
    2024国际生物发酵展畅想未来-势拓伺服科技
    Linux初识+环境部署
    【虹科新品】HK-MR340系列传感器产品介绍合集
    数据分析报告常见步骤
    windows编程之计时器
    PyTorch离线安装
    网络安全笔记3——双钥密码体制
    Minio + Nginx 实现静态资源对外访问
  • 原文地址:https://blog.csdn.net/dante1987/article/details/126243368