SpringBoot学习笔记08——解决Cache缓存同类中调用失败问题

问题描述

今天遇到了一个问题,使用缓存的情况下,如果在缓存服务类方法中调用缓存的方法会调用失败,就是this.缓存方法名,这样使用就不会从缓存中获取数据,而是直接调用缓存方法,错误示例代码如下:

package com.youyou.address.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 这是一个测试缓存service
 *
 * @author 刘朋
 * <br/>date 2018-10-24
 */

@Service
public class CacheService {

    @Autowired
    private CacheService cacheService;

    /**
     * 查询缓存,缓存的名字是testList,用key来标识
     * @param key
     * @return
     */
    @Cacheable(cacheNames = "testList" , key = "#key")
    public List<String> testCache(String key){
        System.out.println("调用了缓存方法");
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add(key);
        return list;
    }

    /**
     * 修改缓存,缓存的名字是testList,用key来标识
     * @param key
     * @return
     */
    @CachePut(cacheNames = "testList" , key = "#key")
    public List<String> testPutCache(String key){
        List<String> stringList = testCache(key);

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add(key);
        return list;
    }
}

上述代码就存在问题,调用testPutCache()时,系统并不会去查询testCache()方法缓存的数据,而是直接调用testCache()方法。

让我们测试一下:

public String testCache(){
        //第一次中缓存中查询
        List<String> test = cacheService.testCache("test");

        //修改缓存中的值
        List<String> test2 = cacheService.testPutCache("test");

        return "";
    }

后台输出结果如下:

会输出两次“调用了缓存方法”,显然时缓存的代码出现了问题。

后来我查阅了一下资料,明白了其中的缘由,简单来讲,在通过注入对象的形式调用方法时,spring会检测到缓存注解,会以aop的形式去执行方法,首先去缓存中查询,如果查询到数据了,就不再执行改方法。如果时在方法中直接调用的话就不能使用aop进行判断了,所以每次都会执行方法体。

解决方法

网上查到的解决方法时这样的“SpringAOP 无法解决,需要使用 AspectJ 来解决!

这个解决方式博主本人没有去测试,而是想到了另一个方法简单易用。

扫描二维码关注公众号,回复: 4040533 查看本文章

我的思路是:既然我们不能直接调用,那么就用注入的方式来解决这个问题就可以了,调用方法的时候使用对象来调用不就没有问题了吗?

接下来带着猜想博主进行了测试,测试代码如下:

package com.youyou.address.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 这是一个测试缓存service
 *
 * @author 刘朋
 * <br/>date 2018-10-24
 */

@Service
public class CacheService {

    @Autowired
    private CacheService cacheService;

    /**
     * 查询缓存,缓存的名字是testList,用key来标识
     * @param key
     * @return
     */
    @Cacheable(cacheNames = "testList" , key = "#key")
    public List<String> testCache(String key){
        System.out.println("调用了缓存方法");
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add(key);
        return list;
    }

    /**
     * 修改缓存,缓存的名字是testList,用key来标识
     * @param key
     * @return
     */
    @CachePut(cacheNames = "testList" , key = "#key")
    public List<String> testPutCache(String key){
        List<String> stringList = cacheService.testCache(key);

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add(key);
        return list;
    }
}

只是在调用testCache()方法时是通过对象进行调用的。

运行结果如下:

只打印了一次“调用了缓存方法”

这说明博主的猜想是正确的。

猜你喜欢

转载自blog.csdn.net/lp840312696/article/details/83501747