springboot2.0 整合 redis

1 redis 整合

1.1 导入jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>



1.2 配置文件
server:
  port: 9006
spring:
  application:
    name: boot-user
  redis:
    host: 192.168.85.198
    database: 0
    port: 6379
    timeout: 2000
    password:
    lettuce: //springboot 2.0 以后不在使用jedis 作为客户端 使用lettuce为底层
      pool:
        max-active: 20  //最大连接数
        max-wait: -1   // 最大阻塞等待时间 -1 表示没有限制
        max-idle: 8  // 最大连接池空闲数量
        min-idle: 0  // 表示最小空闲连接数量
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.85.198:3306/tensquare_user?
         characterEncoding=utf-8&&useSSL=false
    username: root
    password: 123456
  jpa:
    database: mysql
    show-sql: true
    generate-ddl: true


1.3 使用实例

package com.liu.user.service;

import com.liu.common.entity.PageResult;
import com.liu.user.dao.UserDao;
import com.liu.user.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2019/8/15 0015.
 */
@Service
@Slf4j
public class UserService {
package com.liu.user.service;

import com.liu.common.entity.PageResult;
import com.liu.user.dao.UserDao;
import com.liu.user.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2019/8/15 0015.
 * @service  用来标准他是一个service层的实现类
 * @Slf4j  用来生成一个log的内置对象,这样我们就可以使用log 来记录日志
 *
 */
@Service
@Slf4j
public class UserService {

    @Autowired
    private RedisTemplate redisTemplate ;

    @Autowired
    private UserDao userDao;

    public User findById(String id){

       User user = (User) redisTemplate.opsForValue().get("user_"+id);
       if(user ==null){
           log.info("缓存中无数据 {}",user);
           user =userDao.findById(id).get();
           redisTemplate.opsForValue().set("user_"+id,user);
       }

        return user;
    }

    public PageResult findAllByPage(int page, int rows, String query){
        Pageable pageable= PageRequest.of(page-1,rows);
        log.info("query:{}",query);
        Page<User> users = userDao.findSearch(query,pageable);
        log.info("{}",users.getContent());

        return new PageResult(users.getTotalElements(),users.getContent());
    }


}
    @Autowired
    private RedisTemplate redisTemplate ;

    @Autowired
    private UserDao userDao;

    public User findById(String id){

       User user = (User) redisTemplate.opsForValue().get("user_"+id);
       if(user ==null){
           log.info("缓存中无数据 {}",user);
           user =userDao.findById(id).get();
           redisTemplate.opsForValue().set("user_"+id,user);
       }

        return user;
    }

  


}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值