springboot-redis

204 阅读1分钟

背景

实际上,是集成缓存。基本上是傻瓜式操作。

以前,还要封装为一个jar包,还需要高级工程师才能封装这些基础架构的东西。现在不用了,完全傻瓜配置。因为性能的问题,springboot已经帮你解决了。

步骤

1.添加service类
2.开启缓存
3.使用

4.配置ip

添加service类

实现set get方法

package com.wz.bpm.cache;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

/**
 * 缓存服务
 * @author gongzhihao
 *
 */
@Service
public class RedisService {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    public void set(String key, Object value) {
        //更改在redis里面查看key编码问题
        RedisSerializer redisSerializer =new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        ValueOperations<String,Object> vo = redisTemplate.opsForValue();
        vo.set(key, value);
    }

    public Object get(String key) {
        ValueOperations<String,Object> vo = redisTemplate.opsForValue();
        return vo.get(key);
    }
}


开启缓存

添加注解。在启动类添加注解即可。

package com.wz.bpm;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@MapperScan("com.wz.bpm.mapper")
@EnableCaching //添加注解,启用缓存
public class BpmMainApplication {
	private static Logger logger = LoggerFactory.getLogger(BpmMainApplication.class);

	public static void main(String[] args) {
		logger.info("服务器启动。。。");
		SpringApplication.run(BpmMainApplication.class, args);
	}
}

使用

set
get

1.注入数据
2.调用方法

package com.wz.bpm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wz.bpm.bean.Customer;
import com.wz.bpm.bean.RespBean;
import com.wz.bpm.cache.RedisService;
import com.wz.bpm.service.CustomerService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import redis.clients.jedis.Jedis;

@Api(tags = "客户")
@RestController
@RequestMapping("/customer")
public class CustomerController {
	@Autowired
	CustomerService customerService;
	
	@Autowired
	private RedisService redisService;
	
	@ApiOperation("插入单个数据")
	@RequestMapping(method = RequestMethod.POST)
	public RespBean insert(Customer record) {
		int i = customerService.insert(record);
		return RespBean.printResult(i);
	}
	
	@ApiOperation("获取分页")
	@RequestMapping(value="getPage",method = RequestMethod.GET)
	public RespBean getPage(HttpServletRequest request,
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize) {
		//获取token
		String token = request.getHeader("token");
		
		//获取user
//		Jedis jedis = new Jedis("localhost");
//		String userId = jedis.get(token);
		String userId = (String) redisService.get(token);
		Integer userId2 = Integer.valueOf(userId);
		
		//获取分页
		List<Customer> customers= customerService.getPage(userId2,page, pageSize);
		
		//获取总数量
		Integer count = customerService.getPageCount(userId2);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("data", customers);
		map.put("count", count);
		
		return RespBean.ok(map);
	}
}

配置ip

配置文件配置ip port等信息

//application.properties

# redis
spring.redis.database=0
spring.redis.host=192.168.1.45
spring.redis.port=6379
spring.redis.password=
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.min-idle=0

参考

江南一点雨的书

xiaogang.org.cn/articles/20… //单机

juejin.cn/post/684490… //单机和集群

blog.lqdev.cn/2018/07/23/…