springboot 整合redis

之前做SSH的时候整合redis,怎么整合都整合不进去,比较恼火,后来用springboot一下子就整合进去了,觉得这种方式还是比较靠谱,在这里提供两种实现思路,

1.基于注解

首先在pom.xml文件中引入jar包

                <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>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

建实体类

import java.io.Serializable;

/**
 * @author Eric
 * @date create in2018/8/4 12:23
 */
@Data
public class User implements Serializable {

    private static final long serialVersionUID = 8655851615465363473L;
    private Long id;
    private String username;
    private String password;


    public User() {

    }
    public User(Long id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }
}
创建service以及实现类
/**
 * @author Eric
 * @date create in2018/8/4 12:25
 */

public interface UserService {

    /**
     * 删除
     *
     * @param user 用户对象
     * @return 操作结果
     */
    User saveOrUpdate(User user);

    /**
     * 添加
     *
     * @param id key值
     * @return 返回结果
     */
    User get(long id);

    /**
     * 删除
     *
     * @param id key值
     */
    void delete(long id);
}

实现类,比较关键的是这几种注解,通过注解的方式吧数据缓存到redis中,或者取出来,或者直接删除掉

package com.lj.cache.serivce;

import com.lj.cache.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Eric
 * @date create in2018/8/4 12:25
 */
@Service
public class UserServiceImpl implements UserService{


    /**
     * 通过map模拟一个容器,相当于是从数据库查询出来的数据吧
     */
    private static final Map<Long, User> DATABASES = new HashMap<>();

    static {
        DATABASES.put(1L, new User(1L, "u1", "p1"));
        DATABASES.put(2L, new User(2L, "u2", "p2"));
        DATABASES.put(3L, new User(3L, "u3", "p3"));
    }


    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Cacheable(value = "user", key = "#id")
    @Override
    public User get(long id) {
        // TODO 我们就假设它是从数据库读取出来的
        log.info("进入 get 方法");
        return DATABASES.get(id);
    }

    @CachePut(value = "user", key = "#user.id")
    @Override
    public User saveOrUpdate(User user) {
        DATABASES.put(user.getId(), user);
        return user;
    }

    @CacheEvict(value = "user", key = "#id")
    @Override
    public void delete(long id) {
        DATABASES.remove(id);
    }
}

接下来就是controller层的代码,这里用了restFul风格

package com.lj.cache.controller;

import com.lj.cache.entity.User;
import com.lj.cache.serivce.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Eric
 * @date create in2018/8/4 13:59
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login/{id}")
    public String tologin(@PathVariable(value = "id") long id) {
        User user = new User();
        user.setId(id);
        System.out.println("id = " + id);
        userService.saveOrUpdate(user);
        return String.valueOf(id);
    }
    @RequestMapping("logout/{id}")
    public String delete(@PathVariable(value = "id") long id) {
        userService.delete(id);
        return String.valueOf(id);
    }
}

接下来就是测试运行了,我这里用的是postman,

登录redis客户端,查看结果

数据已经存储进去了。大功告成

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值