Shiro使用Redis作为缓存

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介: Shiro使用Redis作为缓存!

Shiro使用redis作为缓存

实现步骤

应用场景:Shiro为每个用户的角色和权限信息提供缓存支持,通过Shiro自己定义的CacheManager实现,默认实现有Ehcache和内存(就是一个Map结构),在应用中通常使用redis作为缓存服务器,因此使用redis来作为shiro的缓存。

优缺点:一般来说缓存放在本地,通过本地内存进行缓存速度更快,但是在分布式环境下,修改了用户权限需要进行同步。将缓存和Session放在统一的地方进行管理可以解决,但是网络请求延迟较高,且容易造成瞬间高并发。
代码分析:

1、实现AbstractCacheManager:

import org.apache.shiro.cache.AbstractCacheManager;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: yykk
 **/
@Component
public class MyCacheManager extends AbstractCacheManager {
    @Autowired
    private MCahce mCahce;
    @Override
    protected Cache createCache(String name) throws CacheException {
        return mCahce;
    }
}

2、实现Shiro定义的Cache接口

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

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

/**
 * @author: yykk
 **/
@Slf4j
@Component
public class MCahce<K,V> implements Cache<K,V> {
    //redis key
    private final K prefix = (K)"shiro_cache";

    @Autowired
    private RedisTemplate redisTemplate;

    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    //------------------------
    @Override
    public V get(K key) throws CacheException {
        Object o = redisTemplate.opsForHash().get(prefix,key);
        return o == null ? null : (V) o;
    }

    @Override
    public V put(K key, V value) throws CacheException {
        V old = this.get(key);
        redisTemplate.opsForHash().put(prefix,key,value);
        return old;
    }

    @Override
    public V remove(K key) throws CacheException {
        V old = this.get(key);
        redisTemplate.opsForHash().delete(prefix,key);
        return old;
    }

    @Override
    public void clear() throws CacheException {
        redisTemplate.delete(prefix);
    }

    @Override
    public int size() {
        return  redisTemplate.opsForHash().size(prefix).intValue();
    }

    @Override
    public Set<K> keys() {
        return redisTemplate.opsForHash().keys(prefix);
    }

    @Override
    public Collection<V> values() {
        return redisTemplate.opsForHash().values(prefix);
    }
}

3、配置shiro注入缓存管理器:

@bean
public AuthorizingRealm myrealm(CacheManager cacheManager)
    {
        log.info("myrealm()");
        AuthorizingRealm realm = new MyRealm();
        realm.setCachingEnabled(true);  //开启缓存
        realm.setCacheManager(cacheManager); //注入缓存管理器
        return realm;
    }

4、Redis序列化

  • jdk序列化

    • new  JdkSerializationRedisSerializer()
  • json序列化

    • GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();

shiro用redis实现缓存机制

shiro使用redis实现缓存机制,对redisTemplate的key可以设置StringRedisSerializer序列化,value的序列化默认,为JdkSerializationRedisSerializer,不要设置为Jackson2JsonRedisSerializer,会报错,具体什么错忘记了,没有截图,反正也是序列化的问题。如果有个redisTemplate的全局序列化设置value为Jackson2JsonRedisSerializer,可在shiro使用redis时,单独设置一下value的序列化方式。

参考文章:

https://blog.csdn.net/cui_xf_/article/details/84503485

https://blog.51cto.com/u_14150615/2356855

https://www.cnblogs.com/jkavor/p/7264007.html

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
19天前
|
canal 缓存 NoSQL
Redis缓存与数据库如何保证一致性?同步删除+延时双删+异步监听+多重保障方案
根据对一致性的要求程度,提出多种解决方案:同步删除、同步删除+可靠消息、延时双删、异步监听+可靠消息、多重保障方案
Redis缓存与数据库如何保证一致性?同步删除+延时双删+异步监听+多重保障方案
|
20天前
|
存储 NoSQL Redis
SpringCloud基础7——Redis分布式缓存,RDB,AOF持久化+主从+哨兵+分片集群
Redis持久化、RDB和AOF方案、Redis主从集群、哨兵、分片集群、散列插槽、自动手动故障转移
SpringCloud基础7——Redis分布式缓存,RDB,AOF持久化+主从+哨兵+分片集群
|
3天前
|
存储 缓存 NoSQL
解决Redis缓存击穿问题的技术方法
解决Redis缓存击穿问题的技术方法
16 2
|
3天前
|
缓存 NoSQL Redis
解决 Redis 缓存穿透问题的有效方法
解决 Redis 缓存穿透问题的有效方法
14 2
|
1月前
|
缓存 NoSQL 关系型数据库
MySQL与Redis缓存一致性的实现与挑战
在现代软件开发中,MySQL作为关系型数据库管理系统,广泛应用于数据存储;而Redis则以其高性能的内存数据结构存储特性,常被用作缓存层来提升数据访问速度。然而,当MySQL与Redis结合使用时,确保两者之间的数据一致性成为了一个重要且复杂的挑战。本文将从技术角度分享MySQL与Redis缓存一致性的实现方法及其面临的挑战。
64 2
|
存储 缓存 NoSQL
Spring Boot2.5 实战 MongoDB 与高并发 Redis 缓存|学习笔记
快速学习 Spring Boot2.5 实战 MongoDB 与高并发 Redis 缓存
Spring Boot2.5 实战 MongoDB 与高并发 Redis 缓存|学习笔记
|
缓存 NoSQL 安全
6.0Spring Boot 2.0实战 Redis 分布式缓存6.0|学习笔记
快速学习6.0Spring Boot 2.0实战 Redis 分布式缓存6.0。
314 0
6.0Spring Boot 2.0实战 Redis 分布式缓存6.0|学习笔记
|
缓存 NoSQL Redis
首页数据显示-添加 redis 缓存(3)| 学习笔记
快速学习 首页数据显示-添加 redis 缓存(3)
142 0
首页数据显示-添加 redis 缓存(3)| 学习笔记
|
缓存 NoSQL Java
首页数据显示-添加 redis 缓存(1) | 学习笔记
快速学习 首页数据显示-添加 redis 缓存(1)
224 0
首页数据显示-添加 redis 缓存(1) | 学习笔记
|
存储 缓存 NoSQL
Redis 缓存|学习笔记
快速学习Redis 缓存
102 0
下一篇
无影云桌面