redisson集成springboot_spring整合redis详解

redisson集成springboot_spring整合redis详解springboot集成redis非常简单1、引入maven依赖redis包<!–springboot整合redis–><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis&lt…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

springboot集成redis非常简单

1、引入maven依赖redis包

<!-- springboot整合 redis -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-redis</artifactId>
</dependency>

2、application.yml配置文件中配置redis环境

spring:
 redis:
 host: localhost
 #password: redispassword
 port: 6379
 pool:
 max-idle: 100
 min-idle: 1
 max-active: 1000
 max-wait: -1

3、书写配置文件RedisConfig

package com.kinder.redis;
import java.lang.reflect.Method;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
	/**
	 * 生成key的策略
	 * 
	 * @return
	 */
	@Bean
	public KeyGenerator keyGenerator() {
		return new KeyGenerator() {
			@Override
			public Object generate(Object target, Method method, Object... params) {
				StringBuilder sb = new StringBuilder();
				sb.append(target.getClass().getName());
				sb.append(method.getName());
				for (Object obj : params) {
					sb.append(obj.toString());
				}
				return sb.toString();
			}
		};
	}
	/**
	 * 管理缓存
	 */
	@Bean
	public CacheManager cacheManager(RedisTemplate redisTemplate) {
		RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
		return rcm;
	}
	/**
	 * RedisTemplate配置
	 */
	@Bean
	public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
		StringRedisTemplate template = new StringRedisTemplate(factory);
		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		template.setValueSerializer(jackson2JsonRedisSerializer);
		template.afterPropertiesSet();
		return template;
	}
}

4、编写redis的service的服务类

package com.kinder.redis;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
@Service
public class RedisService {
	@Autowired
	private RedisTemplate redisTemplate;
	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 写入缓存设置时效时间
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value, Long expireTime) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 批量删除对应的value
	 * 
	 * @param keys
	 */
	public void remove(final String... keys) {
		for (String key : keys) {
			remove(key);
		}
	}
	/**
	 * 批量删除key
	 * 
	 * @param pattern
	 */
	public void removePattern(final String pattern) {
		Set<Serializable> keys = redisTemplate.keys(pattern);
		if (keys.size() > 0)
			redisTemplate.delete(keys);
	}
	/**
	 * 删除对应的value
	 * 
	 * @param key
	 */
	public void remove(final String key) {
		if (exists(key)) {
			redisTemplate.delete(key);
		}
	}
	/**
	 * 判断缓存中是否有对应的value
	 * 
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
		return redisTemplate.hasKey(key);
	}
	/**
	 * 读取缓存
	 * 
	 * @param key
	 * @return
	 */
	public Object get(final String key) {
		ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		return operations.get(key);
	}
	/**
	 * 哈希 添加
	 * 
	 * @param key
	 * @param hashKey
	 * @param value
	 */
	public void hmSet(String key, Object hashKey, Object value) {
		HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
		hash.put(key, hashKey, value);
	}
	/**
	 * 哈希获取数据
	 * 
	 * @param key
	 * @param hashKey
	 * @return
	 */
	public Object hmGet(String key, Object hashKey) {
		HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
		return hash.get(key, hashKey);
	}
	/**
	 * 列表添加
	 * 
	 * @param k
	 * @param v
	 */
	public void lPush(String k, Object v) {
		ListOperations<String, Object> list = redisTemplate.opsForList();
		list.rightPush(k, v);
	}
	/**
	 * 列表获取
	 * 
	 * @param k
	 * @param l
	 * @param l1
	 * @return
	 */
	public List<Object> lRange(String k, long l, long l1) {
		ListOperations<String, Object> list = redisTemplate.opsForList();
		return list.range(k, l, l1);
	}
	/**
	 * 集合添加
	 * 
	 * @param key
	 * @param value
	 */
	public void add(String key, Object value) {
		SetOperations<String, Object> set = redisTemplate.opsForSet();
		set.add(key, value);
	}
	/**
	 * 集合获取
	 * 
	 * @param key
	 * @return
	 */
	public Set<Object> setMembers(String key) {
		SetOperations<String, Object> set = redisTemplate.opsForSet();
		return set.members(key);
	}
	/**
	 * 有序集合添加
	 * 
	 * @param key
	 * @param value
	 * @param scoure
	 */
	public void zAdd(String key, Object value, double scoure) {
		ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
		zset.add(key, value, scoure);
	}
	/**
	 * 有序集合获取
	 * 
	 * @param key
	 * @param scoure
	 * @param scoure1
	 * @return
	 */
	public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
		ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
		return zset.rangeByScore(key, scoure, scoure1);
	}
}

5、操作redis。

package com.kinder.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.kinder.redis.RedisService;
import com.kinder.util.ResultUtil;
/**
 * redis操作
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/demo/redis")
public class RedisController {
	@Autowired
	private RedisService redisService;
	/**
	 * 设置Redis数据
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	@RequestMapping("/setRedis.do")
	@ResponseBody
	public Object setRedis(String key, String value) {
		redisService.set(key, value);
		return ResultUtil.returnSuccess();
	}
	/**
	 * 根据key删除Redis数据
	 * 
	 * @param key
	 * @return
	 */
	@RequestMapping("/delRedis.do")
	@ResponseBody
	public Object delRedis(String key) {
		redisService.remove(key);
		return ResultUtil.returnSuccess();
	}
	/**
	 * 根据key获取Redis数据
	 * 
	 * @param key
	 * @return
	 */
	@RequestMapping("/getRedis.do")
	@ResponseBody
	public Object getRedis(String key) {
		Object value = redisService.get(key);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("value", value);
		return ResultUtil.returnSuccess(map);
	}
}

The end.

欢迎工作一到五年的Java工程师朋友们加入Java架构师:697558955

群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用”没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190752.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 从“挂科大王”到网络安全布道者,云舒的那些“面壁”时刻[通俗易懂]

    从学习、工作和创业经历出发,走近网络安全布道者云舒的人生。

    2022年4月15日
    50
  • Mysql 8.0解压版下载安装及配置教程

    Mysql 8.0解压版下载安装及配置教程首先声明,这里是Mysql8.0.27版本、64位操作系统解压版mysql的下载及安装配置及修改初始密码。如果是其他版本的mysql安装,可以自行查找其他方法,记住搜索的时候最好加上是什么版本的,因为不同版本的mysql安装的时候有些命令是不同的,例如,mysql5.7的就搜索mysql5.7安装。话不多说,接下来就是我的mysql8.0.27版本的教程了。1.下载打开下载地址:https://dev.mysql.com/downloads/mysql/,找到箭头中的文件https:/

    2022年5月8日
    47
  • 【C语言】getchar 函数的正确使用

    【C语言】getchar 函数的正确使用目录一、getchar函数二、缓冲区1、什么是缓冲区2、为什么要存在缓冲区3、缓冲区的类型4、缓冲区的刷新三、getchar函数的正确使用1、getchar的换行问题2、getchar与scanf的混合使用一、getchar函数从上面的介绍来看,我们要正确使用getchar函数,首先得了解什么是缓冲区。二、缓冲区1、什么是缓冲区缓冲区又称为缓存,它是内存空间的一部分。也就是说,在内存空间中预留了一定的存储空间,这些存储空间用来缓冲输入或输出的数据,这部分预留的空间就叫做缓冲区。

    2022年10月19日
    0
  • VS2008序列号[通俗易懂]

    VS2008序列号[通俗易懂]VS2008.NET简体中文版序列号1.VisualStudio2008ProfessionalEdition:XMQ2Y-4T3V6-XJ48Y-D3K2V-6C4WT2.Visual

    2022年7月2日
    33
  • java物联网架构图_java物联网程序设计基础

    java物联网架构图_java物联网程序设计基础物联网架构成长零、说明  说明一下,我写的博客,都是我自己实践后,写下记录,方便以后自己看而已。没有什么系统的教学教程。也没有对底层的原理进行讲解,我觉得这些目前不是我需要注重的,作为一个开发人员,快速实现好需求才是最重要的。而作为一名合格的架构负责人,又必须了解底层,因此需要系统学习的,建议读者看官方文档或者对应的书籍。一、基础  【http://www.cnblogs.com/…

    2022年9月18日
    0
  • OSChina 周六乱弹 ——在你玩的时候,妹子都在干这个[通俗易懂]

    OSChina 周六乱弹 ——在你玩的时候,妹子都在干这个

    2022年3月4日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号