1. 程式人生 > 其它 >SpringBoot中shiro使用redis作為快取機制

SpringBoot中shiro使用redis作為快取機制

技術標籤:SpringBoot學習shiroredisshiro

SpringBoot中shiro使用redis作為快取機制

1.匯入依賴

<!--整合redis-->
        <dependency>
            <
groupId>
org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.2.4.RELEASE</version> </dependency>

2.修改application.yml配置檔案

spring:
  redis:
    port: 6379
    host: 55.165.226.xxx
    database:
0

3.編寫單元測試redis能否連線

@Autowired
    RedisTemplate<String,String> redisTemplate;

    @Test
    public void testRedisConnect(){
        Map<String,String> map=new HashMap<>();
        map.put("ttttt","15");
        redisTemplate.opsForHash().put("haha",
"dierge",map.toString()); Object mmap =new Object(); mmap=redisTemplate.opsForHash().get("haha","dierge"); System.out.println(mmap); }

3.1測試結果:

在這裡插入圖片描述

3.2RDM中結果

在這裡插入圖片描述

4.開發RedisCacheManager類

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.springframework.stereotype.Component;

@Component
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>(cacheName);
    }
}

5.開發RedisCache類

注意在下面的程式碼中,如果直接注入redisTemplate,我的總會報空指標的錯誤,然後我上網搜了一下,也沒能解決,所以就只能使用自己編寫的ApplicationContextUtils工具類來獲取redisTemplate。如果讀者有能解決空指標的辦法或者有實現了的直接注入redisTemplate的例子,可以在評論區中給出,謝謝。

import com.graproject.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;


public class RedisCache<K, V> implements Cache<K, V> {

    private String cacheName;

    public RedisCache() {
    }

    public RedisCache(String cacheName) {
        this.cacheName = cacheName;
    }

    @Override
    public V get(K k) throws CacheException {
        System.out.println("獲取快取:" + k);
        return (V) getRedisTemplate().opsForHash().get(this.cacheName, k.toString());
    }

    @Override
    public V put(K k, V v) throws CacheException {
        System.out.println("設定快取key: " + k + " value:" + v);
        getRedisTemplate().opsForHash().put(this.cacheName, k.toString(), v);
        return null;
    }

    @Override
    public V remove(K k) throws CacheException {
        return (V) getRedisTemplate().opsForHash().delete(this.cacheName, k.toString());
    }

    @Override
    public void clear() throws CacheException {
        getRedisTemplate().delete(this.cacheName);
    }

    @Override
    public int size() {
        return getRedisTemplate().opsForHash().size(this.cacheName).intValue();
    }

    @Override
    public Set<K> keys() {
        return getRedisTemplate().opsForHash().keys(this.cacheName);
    }

    @Override
    public Collection<V> values() {
        return getRedisTemplate().opsForHash().values(this.cacheName);
    }

    private RedisTemplate getRedisTemplate() {
        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}

5.1開發ApplicationContextUtils工具類

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    //根據bean名字獲取工廠中指定bean 物件
    public static Object getBean(String beanName){
        System.out.println("beanName"+beanName);
        Object object=context.getBean(beanName);
        System.out.println("object"+object);
        return context.getBean(beanName);
    }
}

6.在shiro配置類中,去開啟shiro的快取機制

@Bean
    public Realm getRealm(){
        UserRealm userRealm = new UserRealm();

        userRealm.setCachingEnabled(true);

        userRealm.setCacheManager(new RedisCacheManager());//這裡代表使用redis快取。

        userRealm.setAuthenticationCachingEnabled(true);
        userRealm.setAuthenticationCacheName("authenticationCache");

        userRealm.setAuthorizationCachingEnabled(true);
        userRealm.setAuthorizationCacheName("authorizationCache");

        return userRealm;
    }

7.測試

我這裡編寫了一個測試登入的介面,然後登入之後就可以去redis中檢視資料了,然後當你再次登入的時候,去後端檢視日誌,看是否再次訪問了mysql介面,如果沒有進行訪問mysql資料庫,那麼就代表使用redis作為快取機制成功。
在這裡插入圖片描述