八、springboot缓存之Redis

本文详细介绍了如何在SpringBoot应用中集成Redis作为缓存系统,包括无序列化和序列化的步骤。从引入Redis依赖,配置Redis,创建RedisCacheManager,到实现自定义序列化工具MySerializeUtil,确保数据在Redis中的存储和读取。
摘要由CSDN通过智能技术生成

无序列化

在这里插入图片描述

1、引入Redis依赖
 <!--redis整合springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
2、配置Redis依赖
server.port=8080
server.servlet.context-path=/shiro
spring.application.name=shiro


spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp





######Druid监控配置######
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/shiro?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
#dataSource Pool configuration
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000   
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.exceptionSorter=true
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.filters=stat,wall,log4j
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
spring.datasource.useGlobalDataSourceStat=true


mybatis.type-aliases-package=com.hz52.springboot_jsp_shiro.entity
mybatis.mapper-locations=classpath:com/hz52/mapper/*.xml


#设置调试信息
#logging.level.root=debug
logging.level.com.hz52.springboot_jsp_shiro.dao=debug



#Redis
spring.redis.port=6379
spring.redis.host=localhost
spring.redis.password=3.1415926
    #设置0号数据库
spring.redis.database=0 



3、创建RedisCacheManager

在这里插入图片描述

package com.hz52.springboot_jsp_shiro.shiro.cache;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;

/**
 * @Program: springboot_jsp_shiro
 * @Description: 自定义shiro缓存管理器
 * @Author: 52Hz
 * @CreationTime: 2021年10月23日 15:25 星期六
 **/
public class RedisCacheManager implements CacheManager {
   
    
    @Override
    public <K, V> Cache<K, V> getCache(String cacheName) throws CacheException {
   
        System.out.println(cacheName);
        
        return new RedisCache<K,V>();
    }
}

4、创建RedisCache

在这里插入图片描述

package com.hz52.springboot_jsp_shiro.shiro.cache;

import com.hz52.springboot_jsp_shiro.Utils.ApplicationContextUtils;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.Collection;
import java.util.Set;

/**
 * @Program: springboot_jsp_shiro
 * @Description: 自定义redis缓存实现
 * @Author: 52Hz
 * @CreationTime: 2021年10月23日 15:31 星期六
 **/
public class RedisCache<k, v> implements Cache<k, v> {
   

    @Override
    public v get(k k) throws CacheException {
   
        System.out.println("Get key:" + k);
        //获取redis
        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");

        //修改序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return (v) redisTemplate.opsForValue().get(k.toString());
    }

    @Override
    public v put(k k, v v) throws CacheException {
   
        System.out.println("Put key:" + k);
        System.out.println("Put value:" + v);

        //获取redis
        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");
        //修改序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.opsForValue().set(k.toString(), v);

        return null;


    }


    @Override
    public v remove(k k) throws CacheException {
   
        return null;
    }

    @Override
    public void clear() throws CacheException {
   

    }

    @Override
    public int size() {
   
        return 0;
    }

    @Override
    public Set<k> keys() {
   
        return null;
    }

    @Override
    public Collection<v> values() {
   
        return null;
    }
}

5、创建MyByteSource
package com.hz52.springboot_jsp_shiro.shiro.salt;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.CodecSupport;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.util.ByteSource;

import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;

/**
 * @Program: springboot_jsp_shiro
 * @Description:
 * @Author: 52Hz
 * @CreationTime: 2021年10月23日 16:49 星期六
 **/
public class MyByteSource implements ByteSource, Serializable {
   
    private byte[] bytes;
    private String cachedHex;
    private String cachedBase64;

    public MyByteSource() {
   
    }

    public MyByteSource(byte[] bytes) {
   
        this.bytes = bytes;
    }

    public MyByteSource(char[] chars) {
   
        this.bytes = CodecSupport.toBytes(chars);
    }

    public MyByteSource(String string) {
   
        this.bytes = CodecSupport.toBytes(string);
    }

    public MyByteSource(ByteSource source) {
   
        this.bytes = source.getBytes();
    }

    public MyByteSource(File file
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值