org.xlightweb
Class CacheHandler

Package class diagram package CacheHandler
java.lang.Object
  extended by org.xlightweb.CacheHandler
All Implemented Interfaces:
IHttpRequestHandler, ILifeCycle

public final class CacheHandler
extends Object
implements IHttpRequestHandler, ILifeCycle

Cache handler Example:

   RequestHandlerChain chain = new RequestHandlerChain();
   chain.addLast(new CacheHandler(500));  // add a cache handler with max size 500 KB 
   chain.addLast(new FileServiceRequestHandler(basepath));

   HttpServer server = new HttpServer(chain);   
   //...
 
HttpClient will add a cache handler automaically, if the cacheMaxSizeKB is set larger than 0


Field Summary
 
Fields inherited from interface org.xlightweb.IHttpRequestHandler
DEFAULT_EXECUTION_MODE, DEFAULT_INVOKE_ON_MODE, DEFAULT_SYNCHRONIZED_ON_MODE
 
Constructor Summary
CacheHandler(int maxSizeByteKB)
          constructor
 
Method Summary
 List<String> getCacheInfo()
          return the cache info
 int getCountCacheHit()
          returns the number of cache hits
 int getCountCacheMiss()
          returns the number of cache misses
 int getCurrentCacheSizeBytes()
          return the current cache size
 double getCurrentHitRatio()
          returns the current hit ratio
 int getMaxCacheSizeKB()
          return the max cache size
 boolean isSharedCache()
          returns true, if cache is shared
 void onDestroy()
          
 void onInit()
          
 void onRequest(IHttpExchange exchange)
          call back method, which will be called if a request message (header) is received
 void setMaxCacheSizeKB(int sizeByteKB)
          set the max cache size
 void setSharedCache(boolean isSharedCache)
          set true, if cache is shared
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CacheHandler

public CacheHandler(int maxSizeByteKB)
constructor

Parameters:
maxSizeKB - the max cache size
Method Detail

onInit

public void onInit()

Specified by:
onInit in interface ILifeCycle

onDestroy

public void onDestroy()
               throws IOException

Specified by:
onDestroy in interface ILifeCycle
Throws:
IOException

setSharedCache

public void setSharedCache(boolean isSharedCache)
set true, if cache is shared

Parameters:
isSharedCache - true, if cache is shared

isSharedCache

public boolean isSharedCache()
returns true, if cache is shared

Returns:
true, if cache is shared

setMaxCacheSizeKB

public void setMaxCacheSizeKB(int sizeByteKB)
set the max cache size

Parameters:
sizeBytes - the max cache size

getMaxCacheSizeKB

public int getMaxCacheSizeKB()
return the max cache size

Returns:
the max cache size

getCurrentCacheSizeBytes

public int getCurrentCacheSizeBytes()
return the current cache size

Returns:
the current cache size

getCountCacheHit

public int getCountCacheHit()
returns the number of cache hits

Returns:
the number of cache hits

getCurrentHitRatio

public double getCurrentHitRatio()
returns the current hit ratio

Returns:
the current hit ratio

getCountCacheMiss

public int getCountCacheMiss()
returns the number of cache misses

Returns:
the number of cache misses

getCacheInfo

public List<String> getCacheInfo()
return the cache info

Returns:
the cache info

onRequest

public void onRequest(IHttpExchange exchange)
               throws IOException
call back method, which will be called if a request message (header) is received

Specified by:
onRequest in interface IHttpRequestHandler
Parameters:
exchange - the exchange contains the request from the client is used to send the response
Throws:
IOException - if an exception occurred. By throwing this exception an error http response message will be sent by xSocket, if one or more requests are unanswered. The underlying connection will be closed
BadMessageException - By throwing this exception an error http response message will be sent by xSocket, which contains the exception message. The underlying connection will be closed