SpringBoot和Redis整合

30 篇文章 0 订阅

主要介绍用SpringBoot和Redis整合,这里先介绍,单个节点redis整合方式,redis集群可依次类推,但需要自己增加部分代码。

1.引入maven依赖

<!--这个必不可少-->
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
</parent>
<!--redis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.RedisService接口

package com.yarm.redis.service;

public interface RedisService {

	/**
	 * 插入一条数据
	 * @param key
	 * @param value
	 * @param timeOut
	 * @throws Exception
	 */
	public boolean setRedis(String key, String value, Long timeOut) throws Exception;

	/**
	 * 获取一条数据
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public String getRedis(String key) throws Exception;
	
	/**
	 * 更新key
	 * @param key
	 * @param value
	 * @param timeOut
	 * @throws Exception
	 */
	public boolean updateRedis(String key, String value, Long timeOut) throws Exception;
	
}

3.redis接口实现

package com.yarm.redis.service.impl;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.yarm.redis.service.RedisService;

@Service
@SuppressWarnings("rawtypes")
public class RedisServiceImpl implements RedisService{

	@Autowired
	private RedisTemplate redisTemplate;
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean setRedis(String key, String value, Long timeOut) throws Exception{
		boolean result = false;
		ValueOperations<String, String> ops = this.redisTemplate.opsForValue();
		if (!this.redisTemplate.hasKey(key)) {
			ops.set(key, value, timeOut, TimeUnit.SECONDS);
			result = true;
		}
		return result;
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public String getRedis(String key) throws Exception{
		ValueOperations<String, String> ops = this.redisTemplate.opsForValue();
		return ops.get(key);
	}


	@SuppressWarnings("unchecked")
	@Override
	public boolean updateRedis(String key, String value, Long timeOut) throws Exception {
		boolean result = false;
		if (this.redisTemplate.hasKey(key)) {
			ValueOperations<String, String> ops = this.redisTemplate.opsForValue();
			ops.set(key, value, timeOut, TimeUnit.SECONDS);
			result = true;
		}
		return result;
	}

}

4.调用controller

package com.yarm.controller;

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

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

import com.yarm.service.impl.AppRedisService;

@RestController
@RequestMapping(value="redis")
public class RedisController {

	@Autowired
	private AppRedisService redisService;
	
	@RequestMapping(value = "{id}.json", method = RequestMethod.GET)
	public Map<String, Object> getRedisJson(@PathVariable("id") String id){
		Map<String, Object> map = new HashMap<>();
		try {
			String redis = redisService.getRedis(id);
			map.put("code", 200);
			map.put("data", redis);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	@RequestMapping(value = "{id}.ctr", method = RequestMethod.GET)
	public Map<String, Object> getRedisXml(@PathVariable("id") String id){
		Map<String, Object> map = new HashMap<>();
		try {
			String redis = redisService.getRedis(id);
			map.put("code", 200);
			map.put("data", redis);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	@RequestMapping(value = "{key}_{value}.ctr", method = RequestMethod.GET)
	public Map<String, Object> setRedis(@PathVariable("key") String key, @PathVariable("value") String value){
		Map<String, Object> map = new HashMap<>();
		try {
			 redisService.setRedis(key, value);
			map.put("code", 200);
			map.put("data", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	
	@RequestMapping(value = "set.ctr", method = RequestMethod.POST)
	public Map<String, Object> setRedisMap(@RequestBody Map<String,Object> reqMap){
		Map<String, Object> map = new HashMap<>();
		try {
			String key = reqMap.get("key").toString();
			String value = reqMap.get("value").toString();
			
			map.put("code", 200);
			map.put("data", this.redisService.setRedis(key, value));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	
	@RequestMapping(value = "update.ctr", method = RequestMethod.PUT)
	public Map<String, Object> updateRedisMap(@RequestBody Map<String,Object> reqMap){
		Map<String, Object> map = new HashMap<>();
		try {
			String key = reqMap.get("key").toString();
			String value = reqMap.get("value").toString();
			
			map.put("code", 200);
			map.put("data", this.redisService.updateRedis(key, value));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
}

5.springBoot启动main

package com.yarm;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
	
}

6.项目启动之后即可访问相应的controller

如:http://127.0.0.1:8080/redis/2.json

7.项目地址:

https://gitee.com/yarm_git/Spring-Boot-Demo/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值