springboot+redis简单使用

环境搭建

启动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);
	}
}

在这里插入图片描述

发布了5 篇原创文章 · 获赞 0 · 访问量 155
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览