protected PageInfo buildCachedPageInfo(
      HttpServletRequest request, HttpServletResponse response, CacheStatus cacheStatus)
      throws Exception {

    Timer timer = new Timer(getCachedUri(request));
    timer.start();

    String key = calculateKey(request);
    PageInfo pageInfo;
    ValueWrapper element = cacheStatus.valueWrapper;
    log.debug("Serving cached content for {}", key);
    pageInfo = (PageInfo) element.get();

    for (Map.Entry<String, ? extends Serializable> entry :
        pageInfo.getRequestAttributes().entrySet()) {
      request.setAttribute(entry.getKey(), entry.getValue());
    }

    // As the page is cached, we need to add an instance of the associated
    // controller to the request. This is required by GrailsLayoutDecoratorMapper
    // to pick the appropriate layout.
    if (StringUtils.hasLength(getContext().getControllerName())) {
      Object controller = lookupController(getContext().getControllerClass());
      request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    }
    timer.stop(true);
    response.addHeader(X_CACHED, String.valueOf(true));
    return pageInfo;
  }
예제 #2
0
 private Script compileIfNotCompiled(String scriptString, Context ctx) {
   ValueWrapper vw = scriptCache.get(scriptString);
   Script script = null;
   if (vw == null) {
     script = ctx.compileString(scriptString, UUID.randomUUID().toString(), 0, null);
     scriptCache.put(scriptString, script);
   } else {
     script = (Script) vw.get();
   }
   return script;
 }
예제 #3
0
 private Function compileIfNotCompiled(Context ctx, String funcStr) {
   ValueWrapper vw = funtionCache.get(funcStr);
   Function func = null;
   if (vw == null) {
     func = ctx.compileFunction(global, funcStr, null, 1, null);
     funtionCache.put(funcStr, func);
   } else {
     func = (Function) vw.get();
   }
   return func;
 }
 /** Collect agent system data every second. */
 @Scheduled(fixedDelay = 1000)
 public void collectAgentSystemData() {
   Ehcache nativeCache = (Ehcache) agentMonioringTargetsCache.getNativeCache();
   List<String> keysWithExpiryCheck = convert(nativeCache.getKeysWithExpiryCheck());
   for (String each : keysWithExpiryCheck) {
     ValueWrapper value = agentMonioringTargetsCache.get(each);
     if (value != null && value.get() != null) {
       writeObjectToFile(
           getAgentPath(each), getAgentManager().getSystemDataModel((AgentIdentity) value.get()));
     }
   }
 }
  /** Verifies set() and get() of cache objects. */
  @Test
  public void testGetSet() {
    CouchbaseCache cache = new CouchbaseCache(cacheName, client);

    String key = "couchbase-cache-test";
    String value = "Hello World!";
    cache.put(key, value);

    String stored = (String) client.get(key);
    assertNotNull(stored);
    assertEquals(value, stored);

    ValueWrapper loaded = cache.get(key);
    assertEquals(value, loaded.get());
  }
예제 #6
0
  @SuppressWarnings("unchecked")
  public <T> T get(String key) {
    if (!canCache()) return null;
    ValueWrapper value = cache.get(key);
    if (value == null) return null;
    CacheElement ce = (CacheElement) value.get();

    if (!ce.isIndate()) {
      logger.info("clear cache by key: " + ce);
      return null;
    }

    if (logger.isInfoEnabled()) {
      logger.info("get from cache by key: " + ce);
    }

    return (T) ce.getValue();
  }
 /**
  * @param key
  * @return
  */
 public Object getFromCache(String key) {
   logger.info("query obj from cache with key: " + key);
   Cache cache = getCache();
   if (cache == null) {
     return null;
   }
   ValueWrapper wrapper = getCache().get(key);
   if (wrapper == null) {
     logger.info("can not get obj from cache with key : " + key);
     return null;
   }
   Object value = wrapper.get();
   if (value == null) {
     logger.info("can not get obj from cache with key : " + key);
     return null;
   }
   return value;
 }
예제 #8
0
  /**
   * Handles the GET request for downloading an artifact.
   *
   * @param downloadId the generated download id
   * @param response of the servlet
   * @return {@link ResponseEntity} with status {@link HttpStatus#OK} if successful
   */
  @RequestMapping(method = RequestMethod.GET, value = RestConstants.DOWNLOAD_ID_V1_REQUEST_MAPPING)
  @ResponseBody
  public ResponseEntity<Void> downloadArtifactByDownloadId(
      @PathVariable final String downloadId, final HttpServletResponse response) {
    try {
      final ValueWrapper cacheWrapper = cache.get(downloadId);
      if (cacheWrapper == null) {
        LOGGER.warn("Download Id {} could not be found", downloadId);
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
      }

      final DownloadArtifactCache artifactCache = (DownloadArtifactCache) cacheWrapper.get();
      DbArtifact artifact = null;
      switch (artifactCache.getDownloadType()) {
        case BY_SHA1:
          artifact = artifactRepository.getArtifactBySha1(artifactCache.getId());
          break;

        default:
          LOGGER.warn("Download Type {} not supported", artifactCache.getDownloadType());
          break;
      }

      if (artifact == null) {
        LOGGER.warn(
            "Artifact with cached id {} and download type {} could not be found.",
            artifactCache.getId(),
            artifactCache.getDownloadType());
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
      }
      try {
        IOUtils.copy(artifact.getFileInputStream(), response.getOutputStream());
      } catch (final IOException e) {
        LOGGER.error("Cannot copy streams", e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
      }
    } finally {
      cache.evict(downloadId);
    }

    return ResponseEntity.ok().build();
  }