2016-08-05 3 views
4

さまざまなクラスのGETメソッドがいくつかあります。これらすべてのキャッシュを導入する予定です。ジャージーのキャッシュメソッドロジック

ロジックは次のようなものになります。

@GET 
@Path("/route1") { 

    String cacheKey = routePathWithParams; 
    if (cache.get(cacheKey) != null) { 
     return cache.get(cacheKey); 
    } else { 
     // call servcie and get response 
     cache.put(cacheKey, response) 
     return response; 
    } 

} 

私はこのロジックをすべてのGETメソッドに入れたくありません。フックするのに最適な場所はどれですか?

フィルターは使用できますか?

public class CacheFilter implements ContainerRequestFilter, ContainerResponseFilter { 


    public void filter(ContainerRequestContext req) throws IOException { 
     // frame cacheKey from url and params 
     if (cache.get(cacheKey) != null) { 
      // return response from here. How to do it ??? 
     } else { 
      //forward the request How to do it ??? 
     } 
    } 

    public void filter(ContainerRequestContext req, ContainerResponseContext res) { 
     // frame cachekey and put response 
     cache.put(cacheKey, response) 

    } 
} 

これを行うより良い方法があります。 基本的にこの質問は、クライアント側のキャッシュまたはキャッシュヘッダーのユーザーへの送信に関するものではありません。

基本的には、ルートメソッド内で内部サービスコールをキャッシュしようとしています。

+0

とにかく呼ばれているのですか? –

+0

@CássioMazzochiMolinキーのキャッシュ有効期限を設定します。したがって、キーが期限切れになると、私は内部サービスを再度呼び出し、一定間隔でキャッシュします。 – sanjeev

答えて

0

私はこの

public class CachedInterceptor implements WriterInterceptor { 

    @Override 
    public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { 
     OutputStream outputStream = context.getOutputStream(); 

     String key = "key"; 

     try { 
      byte[] entity = cache.get(key); 

      if (entity == null) { 
       ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
       context.setOutputStream(buffer); 
       context.proceed(); 

       entity = buffer.toByteArray(); 

       cache.put(key, entity); 
      } 

      outputStream.write(entity); 
     } finally { 
      context.setOutputStream(outputStream); 
     } 
    } 
} 

ような何かをしようとしていますが、アクションがキャッシュを無効にすることを計画するにはどうすればよい

関連する問題