/** 拦截 Service/DAO 的方法,并查找该结果是否存在,如果存在就返回 cache 中的值, 否则,返回数据库查询结果,并将查询结果放入 cache */
  @Override
  public Object invoke(MethodInvocation invocation) throws Throwable {

    String targetName = invocation.getThis().getClass().getName();

    String methodName = invocation.getMethod().getName();

    Object[] arguments = invocation.getArguments();

    Object result;

    String cacheKey = getCacheKey(targetName, methodName, arguments);

    logger.info("从[" + cache.getName() + "]查询key:[" + cacheKey + "]的缓存]");

    //    System.out.println("从["+cache.getName()+"]查询key:["+cacheKey+"]的缓存]");

    Element element = cache.get(cacheKey);

    if (element == null) {

      result = invocation.proceed();

      logger.info("key:[" + cacheKey + "]无缓存,创建缓存为[" + result + "]");

      //      System.out.println("key:["+cacheKey+"]无缓存,创建缓存为["+result+"]");

      element = new Element(cacheKey, (Serializable) result);

      cache.put(element);
    }

    return element.getObjectValue();
  }
    private void testGetKeysMethods(Cache[] caches) {
      for (Cache c : caches) {
        String name = c.getName();
        if (!name.equals("dcv2") && !name.equals("classic")) {
          throw new AssertionError(name);
        }

        verifyKeys(name, c.getKeys(), "classic".equals(name) ? 2 : 1);
        verifyKeys(name, c.getKeysWithExpiryCheck(), 1);
      }
    }
  public Object invoke(MethodInvocation invocation) throws Throwable {

    String targetName = invocation.getThis().getClass().getName();

    String methodName = invocation.getMethod().getName();

    Object[] arguments = invocation.getArguments();

    Object result;

    String cacheKey = getCacheKey(targetName, methodName, arguments);

    Element element = null;

    synchronized (this) {
      element = cache.get(cacheKey);

      if (element == null) {

        log.info(cacheKey + "加入到缓存: " + cache.getName());

        // 调用实际的方法

        result = invocation.proceed();

        element = new Element(cacheKey, (Serializable) result);

        cache.put(element);

      } else {

        log.info(cacheKey + "使用缓存: " + cache.getName());
      }
    }

    return element.getValue();
  }
  public void reconfigureCaches(URL configurationURL) {
    Configuration configuration =
        EhcacheConfigurationUtil.getConfiguration(configurationURL, _clusterAware);

    Map<String, CacheConfiguration> cacheConfigurations = configuration.getCacheConfigurations();

    for (CacheConfiguration cacheConfiguration : cacheConfigurations.values()) {

      Cache cache = new Cache(cacheConfiguration);

      PortalCache portalCache = addCache(cache.getName(), cache);

      if (portalCache == null) {
        _log.error("Failed to override cache " + cacheConfiguration.getName());
      }
    }
  }
  /** 拦截Service/DAO的方法,并查找该结果是否存在,如果存在就返回cache中的值, 否则,返回数据库查询结果,并将查询结果放入cache */
  public Object invoke(MethodInvocation invocation) throws Throwable {
    String targetName = invocation.getThis().getClass().getName();
    String methodName = invocation.getMethod().getName();
    Object[] arguments = invocation.getArguments();
    Object result;

    logger.debug("Find object from cache is " + cache.getName());

    String cacheKey = getCacheKey(targetName, methodName, arguments);
    Element element = cache.get(cacheKey);

    if (element == null) {
      logger.debug("Hold up method , Get method result and create cache........!");
      result = invocation.proceed();
      element = new Element(cacheKey, (Serializable) result);
      cache.put(element);
    }
    return element.getValue();
  }
Beispiel #6
0
  public void add(Query query) {
    String name = query.getQid().getId();
    if (!this.manager.cacheExists(name) && query.isCacheable()) {

      CacheConfiguration config = defaultConfig.clone();
      config.setName(name);
      config.setEternal(query.isEternal());
      config.setTimeToLiveSeconds(query.getCacheTime());

      Cache c = new Cache(config);
      this.manager.addCache(c);

      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Cache %s created: eternal = %s, cacheTime = %d",
                c.getName(), query.isEternal(), query.getCacheTime()));
      }
    }
  }
 public ClientContextService() {
   ClassLoader classLoader = getClass().getClassLoader();
   cacheManager = CacheManager.newInstance(classLoader.getResource("ehcache.xml").getFile());
   cache = cacheManager.getCache("clientsCache");
   System.out.println("ready: " + cache.getName());
 }
 public String getName() {
   return cache.getName();
 }