在 Jersey 中缓存实现的 Web 服务
Caching in Jersey Implemented Web Service
目前,我的 Jersey 实现的 Rest Web 服务方法能够根据 userId 从数据库 table 中获取字符串列表。服务将一次只发送一个字符串给用户客户端调用。剩余的应该在缓存中存储一段时间并为该用户标识。因此,当下次调用 Web 服务时,它会检查用户 ID 和时间限制是进行新的数据库查询还是使用缓存的结果将结果发回。
请帮助我在这里使用缓存方法。我完成了我的服务方法。
我的示例 Web 服务方法。如何在此处放置缓存技术:
@GET
@Path("/CacheService/{userId}")
public Response invokeCacheService(@PathParam("id") String userId, @Context Request req){
Response.ResponseBuilder rb = null;
ArrayList songList = new ArrayList();
songList = UserService.getPlaylistSongs(userId);
rb = Response.ok(songList);
return rb.build();
}
这里有一个实用程序 class 以便在服务器端进行 HTTP 缓存控制:
public class HttpCacheRizze extends CacheControl {
public static CacheControl minutesSecondesMilliseconds(int min, int sec, int milli){
HttpCacheRizze cc=new HttpCacheRizze();
cc.setMaxAge(min*60+sec+milli/1000);
cc.setPrivate(true);
return cc;
}
public static EntityTag etag(String tag) {
EntityTag etag = new EntityTag(DigestUtils.sha256Hex(tag));
return etag;
}
/**
*
* @param req
* @param tag
* @param timeout
* @return response if isUnderCache or null if not
*/
public static Response getCachedResponseMilliseconds(Request req, String tag,int timeoutMs){
Response.ResponseBuilder rb=null;
EntityTag etag = etag(tag);
if(req!=null){
rb = req.evaluatePreconditions(new Date(),etag);
if (rb != null){
return rb.cacheControl(HttpCacheRizze.minutesSecondesMilliseconds(0,0,timeout)).tag(etag).build();
}
}
return null;
}
/**
*
* @param status
* @param entity
* @param tag
* @param timeout
* @return response will be cached
*/
public static Response getCacheInvalidatedResponse(int status, String entity, String tag, int timeoutMs){
//if entity is null, force status to 204 (empty)
if(status==204 || entity==null || entity.compareTo("null")==0 || entity.compareTo("{\"null\"}")==0) {
status=204;
entity=null;
}
return Response.status(status).entity(entity)
.cacheControl(HttpCacheRizze.minutesSecondesMilliseconds(0, 0, timeoutMs))
.tag(etag(tag))
.build();
}
}
您的代码将如下所示:
@GET
@Path("/CacheService/{userId}")
public Response invokeCacheService(@PathParam("id") String userId, @Context Request req){
//the key of a good cache control technique, is to : be quick in order to determine if present or not in cache,
//and to try to avoid the maximum data processing in order to retrieve fromthe cache (example avoid performing getPlaylistSong under cache
int TTL_CACHE_SONGS=10000; //in ms
String tag = "CacheService"+userid";
//is under cache ?
Response r = HttpCacheRizze.getCachedResponseMilliseconds(req, tag, TTL_CACHE_SONGS);
if(r!=null){
// under cache
return r;
}
// cache is not present or need to be refreshed
ArrayList songList = new ArrayList();
songList = UserService.getPlaylistSongs(userId);
int status = 200;
//catch here errors .... empty....
if(songList==null || songList.size()==0 )
status = 204;
r = HttpCacheRizze.getCacheInvalidatedResponse(status, new Gson().toJson(songList), tag, TTL_CACHE_SONGS);
return r;
}
目前,我的 Jersey 实现的 Rest Web 服务方法能够根据 userId 从数据库 table 中获取字符串列表。服务将一次只发送一个字符串给用户客户端调用。剩余的应该在缓存中存储一段时间并为该用户标识。因此,当下次调用 Web 服务时,它会检查用户 ID 和时间限制是进行新的数据库查询还是使用缓存的结果将结果发回。
请帮助我在这里使用缓存方法。我完成了我的服务方法。
我的示例 Web 服务方法。如何在此处放置缓存技术:
@GET
@Path("/CacheService/{userId}")
public Response invokeCacheService(@PathParam("id") String userId, @Context Request req){
Response.ResponseBuilder rb = null;
ArrayList songList = new ArrayList();
songList = UserService.getPlaylistSongs(userId);
rb = Response.ok(songList);
return rb.build();
}
这里有一个实用程序 class 以便在服务器端进行 HTTP 缓存控制:
public class HttpCacheRizze extends CacheControl {
public static CacheControl minutesSecondesMilliseconds(int min, int sec, int milli){
HttpCacheRizze cc=new HttpCacheRizze();
cc.setMaxAge(min*60+sec+milli/1000);
cc.setPrivate(true);
return cc;
}
public static EntityTag etag(String tag) {
EntityTag etag = new EntityTag(DigestUtils.sha256Hex(tag));
return etag;
}
/**
*
* @param req
* @param tag
* @param timeout
* @return response if isUnderCache or null if not
*/
public static Response getCachedResponseMilliseconds(Request req, String tag,int timeoutMs){
Response.ResponseBuilder rb=null;
EntityTag etag = etag(tag);
if(req!=null){
rb = req.evaluatePreconditions(new Date(),etag);
if (rb != null){
return rb.cacheControl(HttpCacheRizze.minutesSecondesMilliseconds(0,0,timeout)).tag(etag).build();
}
}
return null;
}
/**
*
* @param status
* @param entity
* @param tag
* @param timeout
* @return response will be cached
*/
public static Response getCacheInvalidatedResponse(int status, String entity, String tag, int timeoutMs){
//if entity is null, force status to 204 (empty)
if(status==204 || entity==null || entity.compareTo("null")==0 || entity.compareTo("{\"null\"}")==0) {
status=204;
entity=null;
}
return Response.status(status).entity(entity)
.cacheControl(HttpCacheRizze.minutesSecondesMilliseconds(0, 0, timeoutMs))
.tag(etag(tag))
.build();
}
}
您的代码将如下所示:
@GET
@Path("/CacheService/{userId}")
public Response invokeCacheService(@PathParam("id") String userId, @Context Request req){
//the key of a good cache control technique, is to : be quick in order to determine if present or not in cache,
//and to try to avoid the maximum data processing in order to retrieve fromthe cache (example avoid performing getPlaylistSong under cache
int TTL_CACHE_SONGS=10000; //in ms
String tag = "CacheService"+userid";
//is under cache ?
Response r = HttpCacheRizze.getCachedResponseMilliseconds(req, tag, TTL_CACHE_SONGS);
if(r!=null){
// under cache
return r;
}
// cache is not present or need to be refreshed
ArrayList songList = new ArrayList();
songList = UserService.getPlaylistSongs(userId);
int status = 200;
//catch here errors .... empty....
if(songList==null || songList.size()==0 )
status = 204;
r = HttpCacheRizze.getCacheInvalidatedResponse(status, new Gson().toJson(songList), tag, TTL_CACHE_SONGS);
return r;
}