DAO层中的@Cacheable未被触发(Spring / Reddis)

时间:2016-11-18 23:17:14

标签: java spring-mvc caching proxy reddison

在代理模式下,我无法在DAO层内缓存内部方法。

我知道在代理模式下,只拦截通过代理进入的外部方法调用。但是,我想避免必须切换到AspectJ模式,并想知道是否存在任何其他工作。

我正在下面显示我的代码,并想知道我可以添加哪些更改,以使此过程有效。

- 注意我正在使用招摇来记录我的代码

- 另请注意我的代码已被淡化....原因显而易见

//控制器

@RestController
@Api(produces = "application/json", protocols = "https", tags =  "Securities", description = "Securities  information")
public class SecuritiesInfoController extends Controller {

private SecuritiesInfoManager _securitiesInfoManager = new SecuritiesInfoManager();

@RequestMapping(value = "/security", method = RequestMethod.GET)
    public List<SecuritiesInfo> getAll(){ 
   return _securitiesInfoManager.getAll(); 
    }
}

//服务

public class SecuritiesInfoManager extends Manager {

private SecuritiesInfoDAO _securitiesDAO = new SecuritiesInfoDAO();

public List<SecuritiesInfo> getAll() {
    return _securitiesDAO.getAll();
}
}

// DAO

public class SecuritiesInfoDAO extends DAO  {

private static String securitiesTable = "Securities";

@SecuritiesInfoDAOInterface

public List<SecuritiesInfo> getAll() {
 //Magic
}
    }

//接口

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Cacheable(cacheNames = "SecuritiesInfo",cacheManager="cacheManager",
keyGenerator="keyGenerator" )
public @interface SecuritiesInfoDAOInterface {

}

// CacheConfig

@Configuration
//@EnableCaching(mode = AdviceMode.PROXY)
@EnableCaching(proxyTargetClass = true)
//@EnableCaching

public class CacheConfig extends CachingConfigurerSupport {

@Bean
public SecuritiesInfoDAO myService() {
    // configure and return a class having @Cacheable methods
    return new SecuritiesInfoDAO();
}
  @Bean
  public JedisConnectionFactory redisConnectionFactory() {
    JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();

    // Defaults
    redisConnectionFactory.setHostName("Nope");
    redisConnectionFactory.setPort(LoL);
    System.out.println("IN CONNTECTION");
    redisConnectionFactory.setPassword("Please help me :)");
    return redisConnectionFactory;
  }


  @Bean
  public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
      System.out.println("cf: "+cf.toString());
    RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
    redisTemplate.setConnectionFactory(cf);
    return redisTemplate;
  }

  /*
  @Primary
  @Bean
  public RedisTemplate<String,ExpiringSession> redisTemplate2(RedisConnectionFactory connectionFactory) {
      RedisTemplate<String, ExpiringSession> template = new RedisTemplate<String, ExpiringSession>();

      template.setHashValueSerializer(new LdapFailAwareRedisObjectSerializer());

      template.setConnectionFactory(connectionFactory);
      return template;
  }
  */


  @Bean
  public CacheManager cacheManager(RedisTemplate<String, String> redisTemplate) {
      System.out.println("IN CACHE MANAGER");
    RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);

    // Number of seconds before expiration. Defaults to unlimited (0)
   // cacheManager.setDefaultExpiration(300);
    return cacheManager;
  }

  @Bean
  public KeyGenerator keyGenerator() {
    return new KeyGenerator() {
      @Override
      public Object generate(Object o, Method method, Object... objects) {
        // This will generate a unique key of the class name, the method name,
        // and all method parameters appended.
        StringBuilder sb = new StringBuilder();
        sb.append(o.getClass().getName());
        sb.append(method.getName());
        for (Object obj : objects) {
          sb.append(obj.toString());
        }
        System.out.println(sb.toString());
        return sb.toString();
      }
    };
  }

1 个答案:

答案 0 :(得分:1)

所以我想出了答案。事实证明我没有正确实现/实例化接口。

首先,我必须在我的控制器中@Autowire我的经理班。然后@autowire我的经理中的接口类。

有关更详细的解决方案,我将修改后的代码放在下面。

//控制器

@RestController
@Api(produces = "application/json", protocols = "https", tags =    "Securities", description = "Securities  information")
public class SecuritiesInfoController extends Controller {

@Autowired
private SecuritiesInfoManager _securitiesInfoManager = new   SecuritiesInfoManager();

@RequestMapping(value = "/security", method = RequestMethod.GET)
   public List<SecuritiesInfo> getAll(){ 
return _securitiesInfoManager.getAll(); 
 }
}

//服务

public class SecuritiesInfoManager extends Manager {

 @Autowired
    public void setSecuritiesInfoDAOInterface(SecuritiesInfoDAOInterface _securitiesInfoDAOInterface) {
        this._securitiesInfoDAOInterface = _securitiesInfoDAOInterface;
    }

public List<SecuritiesInfo> getAll() {
return _securitiesInfoDAOInterface.getAll();
 }
}

// DAO

public class SecuritiesInfoDAO extends DAO implements SecuritiesInfoDAOInterface {

private static String securitiesTable = "Securities";

@Override

public List<SecuritiesInfo> getAll() {
 //Magic
 }

}

//接口

public interface SecuritiesInfoDAOInterface {
    @Cacheable(cacheNames = "SecuritiesInfo",cacheManager="cacheManager", keyGenerator="keyGenerator" )
    List<SecuritiesInfo> getAll();
 }
}

// CacheConfig

@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
@Bean
public SecuritiesInfoManager myService() {
    // configure and return a class having @Cacheable methods
    return new SecuritiesInfoManager();
 }
//rest same as before
}

// WebConfig

@Configuration
@ComponentScan(basePackages = {"package name"})
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    AntPathMatcher matcher = new AntPathMatcher();
    matcher.setCaseSensitive(false);
    configurer.setPathMatcher(matcher);
  }
 }