我在Eclipse中创建了一个项目。我编写了一个名为DemoCache的类,该类使用JCS加载缓存。下面是代码段。
package com.test;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.jcs.access.exception.CacheException;
import com.test.City;
public class DemoCache {
private CacheAccess<String, City> cache = null;
public static void main(String[] args) {
DemoCache demo=new DemoCache();
demo.loadData();
}
public DemoCache(){
try
{
cache = JCS.getInstance( "city" );
}
catch ( CacheException e )
{
System.out.println( String.format( "Problem initializing cache: %s", e.getMessage() ) );
}
}
public void putInCache( City city )
{
String key = city.name;
try
{
cache.put( key, city );
}
catch ( CacheException e )
{
System.out.println( String.format( "Problem putting city %s in the cache, for key %s%n%s",
city.name, key, e.getMessage() ) );
}
}
public City retrieveFromCache( String cityKey )
{
return cache.get( cityKey );
}
public void loadData(){
City zurich = new City( "Zürich", "Switzerland", 366765 );
putInCache( zurich );
City berlin = new City( "Berlin", "Germany", 3502000 );
putInCache( berlin );
City johannesburg = new City( "Johannesburg", "South Africa", 12200000 );
putInCache( johannesburg );
}
}
另一个类是更新下面的代码的缓存
package com.test;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.jcs.access.exception.CacheException;
public class UpdateCache {
private CacheAccess<String, City> cache = null;
public static void main(String[] args) {
UpdateCache updateCache=new UpdateCache();
updateCache.modifyCache();
}
public UpdateCache(){
try
{
cache = JCS.getInstance( "city" );
}
catch ( CacheException e )
{
System.out.println( String.format( "Problem initializing cache: %s", e.getMessage() ) );
}
}
public void putInCache( City city )
{
String key = city.name;
try
{
cache.put( key, city );
}
catch ( CacheException e )
{
System.out.println( String.format( "Problem putting city %s in the cache, for key %s%n%s",city.name, key, e.getMessage() ) );
}
}
public void removeFromCache( String key )
{
try
{
cache.remove(key);
}
catch ( CacheException e )
{
System.out.println( String.format( "Problem putting city %s in the cache, for key %s%n%s",key, e.getMessage() ) );
}
}
public void modifyCache(){
City zurich = new City( "LA", "America", 5000000 );
putInCache( zurich );
//removeFromCache("LA");
}
}
有第三个程序生成线程以从缓存中检索值
package com.test;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.jcs.access.exception.CacheException;
public class RetriveThread implements Runnable {
private CacheAccess<String, City> cache = null;
public RetriveThread(){
try {
cache = JCS.getInstance("city");
} catch (CacheException e) {
System.out.println(String.format("Problem initializing cache: %s",
e.getMessage()));
}
}
public City retrieveFromCache(String cityKey) {
return cache.get(cityKey);
}
@Override
public void run() {
try{
int i;
for(i=0;i<=5;i++){
Thread.sleep(1000);
System.out.println("------------------------------------------------------------");
City retrievedCity1 = retrieveFromCache("LA");
if (retrievedCity1 != null) {
System.out.println(retrievedCity1.toString());
} else {
System.out.println("No object was found in the cache for the key \"LA\"");
}
City retrievedCity2 = retrieveFromCache("Zürich");
if (retrievedCity2 != null) {
System.out.println(retrievedCity2.toString());
} else {
System.out.println("No object was found in the cache for the key \"Zürich\"");
}
System.out.println("------------------------------------------------------------");
if(i==5){
i=0;
System.out.println("=========================================================");
continue;
}
}
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
public static void main(String arg[]){
RetriveThread retriveThred=new RetriveThread();
Thread t=new Thread(retriveThred);
t.start();
}
}
我首先执行程序以加载缓存,然后执行线程程序以检索缓存值。然后,我执行更新程序以执行高速缓存,在该高速缓存中,我首先在高速缓存中添加了“ LA”值,但是线程程序未显示LA值详细信息,而是给出了“未找到对象”消息。但是,如果我终止检索线程程序并执行,它将给出“ LA”值。是指检索程序无法识别缓存中的更新。我想实现无论何时执行检索线程程序,它都会自动使用缓存更新进行更新并开始检索更新后的值。如何做到这一点,请帮助。