springboot+redis简单使用

本文介绍了如何在SpringBoot项目中集成并使用Redis,包括Redis的环境搭建、启动,引入相关jar包,配置参数,设置bean,创建工具类以及编写使用示例。
摘要由CSDN通过智能技术生成

环境搭建

启动redis

1、下载redis window版本插件包。
链接:https://pan.baidu.com/s/1WFLYk-VPn2vZpPBw0O2m7Q
提取码:jfse
2、打开cmd控制台,切换到redis目录,redis-server.exe启动
在这里插入图片描述

引入redis相关jar包

 <!-- redis依赖 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!-- jedis客户端 -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.5.0</version>
    </dependency>

redis参数配置

#redis
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
#spring.redis.password=
spring.redis.timeout=1000
spring.redis.pool.max-active=200
spring.redis.pool.max-wait=6000
spring.redis.pool.max-idle=10
spring.redis.pool.min-idle=0
#redis的session存储
spring.session.store-type=redis
spring.session.timeout=10
server.servlet.session.timeout=10

redis bean配置

package com.li.springboot.readis;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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;

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
	
	@Bean
    public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
        CacheManager cacheManager = new RedisCacheManager(redisTemplate);
        return cacheManager;
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }
}

redis工具类

package com.li.springboot.readis;

import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

@Component
public class IRedisService {
	
	@Resource
    private RedisTemplate<String,Object> redisTemplate;

	/**
	 * 写入缓存
	 * @param key
	 * @param value
	 * @return
	 */
    public boolean set(String key, Object value) {
        try{
        	StringRedisSerializer redisSerializer = new StringRedisSerializer();
            redisTemplate.setKeySerializer(redisSerializer);
            ValueOperations<String,Object> vo = redisTemplate.opsForValue();
            vo.set(key, value);
            return true;
        }catch(Exception e){
        	throw new RuntimeException("写入redis缓存失败!"+e.getMessage());
        }
    }
    
    /**
     * 读取缓存值
     * @param key
     * @return
     */
    public Object get(String key) {
    	try{
    		ValueOperations<String,Object> vo = redisTemplate.opsForValue();
            return vo.get(key);
        }catch(Exception e){
        	throw new RuntimeException("读取redis缓存失败!"+e.getMessage());
        }
    }
}

使用demo

在这里插入图片描述

项目启动类

package com.li.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@MapperScan("com.li.springboot.test.mapper")  //mybatis映射扫描
@ServletComponentScan                         //开启filter过滤扫描
public class SpringApplicatonRun {

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

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值