내 응용 프로그램에서 작동하는 Guava 캐싱을 얻으려고합니다. 다양한 온라인 소스 (문서, 블로그, 기사 등)에서맞춤 POJO 용 Guava 캐시
// Here the keys are the User.getId() and the values are the respective User.
Map<Long, User> userCache = new HashMap<Long, User>();
: 특히, 나는 기본적으로지도처럼 동작 캐시를 찾고 있어요
// My POJO.
public class User {
Long id;
String name;
// Lots of other properties.
}
public class UserCache {
LoadingCache _cache;
UserCacheLoader loader;
UserCacheRemovalListener listener;
UserCache() {
super();
this._cache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterAccess(30, TimeUnit.SECONDS)
.removalListener(listener)
.build(loader);
}
User load(Long id) {
_cache.get(id);
}
}
class UserCacheLoader extends CacheLoader {
@Override
public Object load(Object key) throws Exception {
// ???
return null;
}
}
class UserCacheRemovalListener implements RemovalListener<String, String>{
@Override
public void onRemoval(RemovalNotification<String, String> notification) {
System.out.println("User with ID of " + notification.getKey() + " was removed from the cache.");
}
}
하지만 난 아니에요 어떻게/어디에 키가 Long
유형이어야하고 캐시 된 값이 User
인스턴스 여야하는지 지정하십시오. 또한 store(User)
(기본적으로 Map#put(K,V)
) 메소드와 getKeys()
메소드를 구현하여 캐시의 모든 Long
키를 반환합니다. 내가 어디에서 잘못 왔는지에 대한 어떤 생각?