使用监听器来实现资源的统一管理

使用监听器来实现redis缓存
实体类

package com.itheima.pojo;

import lombok.Data;

import java.io.Serializable;
@Data
public class User implements Serializable {
     
    private int id;
    private String username;
    private String password;
    private String name;

    public User(int id, String username, String password, String name) {
     
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
    }

    public User() {
     
    }

    public int getId() {
     
        return id;
    }

    public void setId(int id) {
     
        this.id = id;
    }

    public String getUsername() {
     
        return username;
    }

    public void setUsername(String username) {
     
        this.username = username;
    }

    public String getPassword() {
     
        return password;
    }

    public void setPassword(String password) {
     
        this.password = password;
    }

    public String getName() {
     
        return name;
    }

    public void setName(String name) {
     
        this.name = name;
    }
}

mapper

package com.itheima.mapper;

import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
     
    @Select("select * from user;")
    List<User> findAll();
}

service以及实现类,实现类主要是进行业务的处理,这里使用的是redisTemplate对象,先通过查找redis缓存里面是否有数据,有的话直接返回,没有的话从数据库中查找数据,并通过redisTemplate对象将他设置到mysql数据库中

package com.itheima.service;

import com.itheima.pojo.User;

import java.util.List;

public interface UserService {
     
    List<User> findAll();
}

package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImple implements UserService {
     
//    @Resource

    //    private UserMapper userMapper;
//
//    @Override
//    public List findAll() {
     
//        return userMapper.findAll();
//    }
//    public static final String str = UserMapper.class.getName()+"."+"findAll";
    @Resource
    private UserMapper userMapper;
    public static final String str = UserMapper.class.getName()+"."+"findAll";
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public List<User> findAll() {
     
        List<User> user = (List<User>) redisTemplate.boundValueOps(str).get();
        if(user == null) {
     
            user = userMapper.findAll();
            redisTemplate.boundValueOps(str).set(user);
            System.out.println("从mysql数据库中获取数据");
        }else {
     
            System.out.println("从redis数据库中获取数据");
        }
        return user;
    }
}

监听器的功能的使用,使用监听器来进行资源的统一管理,多用户访问的时候可以提高效率,直接从缓存中取出数据,将数据设置到redis缓存中去

package com.itheima.listener;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.impl.UserServiceImple;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class RedisCacheInitialzerListener implements ApplicationRunner {
     
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserMapper userMapper;
    @Override
    public void run(ApplicationArguments args) throws Exception {
     
        //设置直接从缓存中获取数据
        List<User> users = userMapper.findAll();
        if(users != null) {
     
            redisTemplate.boundValueOps(UserServiceImple.str).set(users);
            System.out.println("监听器初始化redis缓存了");
        }
    }
}

控制器,一般进行跳转,不进行业务的处理

package com.itheima.controller;

import com.itheima.pojo.User;
import com.itheima.service.impl.UserServiceImple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {
     
    @Resource
    private UserServiceImple userServiceImple;
//    @Resource
//    private RedisTemplate redisTemplate;

//    @RequestMapping("/user/findAll")
//    public List findAll() {
     
//        String str = userServiceImple.getClass().getName() + "findAll";
//        List users = (List) redisTemplate.boundValueOps(str).get();
//        if(users == null) {
     
//            users = userServiceImple.findAll();
//            redisTemplate.boundValueOps(str).set(users);
//            System.out.println("从mysql数据库中获取数据");
//        }else {
     
//            System.out.println("从redis数据库中获取数据");
//        }
//        return users;
//    }
    @RequestMapping("/user/findAll")
    public List<User> findAll() {
     
        return userServiceImple.findAll();
    }
}

你可能感兴趣的:(使用监听器来实现资源的统一管理)