Fix CacheStore errors from checkstyle plugin 107 left
This commit is contained in:
@ -35,20 +35,34 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class CacheStore {
|
public class CacheStore {
|
||||||
|
/**
|
||||||
|
* Cach capacity.
|
||||||
|
*/
|
||||||
private static final int CAPACITY = 3;
|
private static final int CAPACITY = 3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lru cache see {@link LruCache}.
|
||||||
|
*/
|
||||||
private static LruCache cache;
|
private static LruCache cache;
|
||||||
|
/**
|
||||||
|
* DbManager.
|
||||||
|
*/
|
||||||
private DbManager dbManager;
|
private DbManager dbManager;
|
||||||
|
|
||||||
public CacheStore(DbManager dbManager) {
|
/**
|
||||||
this.dbManager = dbManager;
|
* Cache Store.
|
||||||
|
* @param dataBaseManager {@link DbManager}
|
||||||
|
*/
|
||||||
|
public CacheStore(final DbManager dataBaseManager) {
|
||||||
|
this.dbManager = dataBaseManager;
|
||||||
initCapacity(CAPACITY);
|
initCapacity(CAPACITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init cache capacity.
|
* Init cache capacity.
|
||||||
|
* @param capacity int
|
||||||
*/
|
*/
|
||||||
public void initCapacity(int capacity) {
|
public void initCapacity(final int capacity) {
|
||||||
if (cache == null) {
|
if (cache == null) {
|
||||||
cache = new LruCache(capacity);
|
cache = new LruCache(capacity);
|
||||||
} else {
|
} else {
|
||||||
@ -58,8 +72,10 @@ public class CacheStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get user account using read-through cache.
|
* Get user account using read-through cache.
|
||||||
|
* @param userId {@link String}
|
||||||
|
* @return {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
public UserAccount readThrough(String userId) {
|
public UserAccount readThrough(final String userId) {
|
||||||
if (cache.contains(userId)) {
|
if (cache.contains(userId)) {
|
||||||
LOGGER.info("# Found in Cache!");
|
LOGGER.info("# Found in Cache!");
|
||||||
return cache.get(userId);
|
return cache.get(userId);
|
||||||
@ -72,8 +88,9 @@ public class CacheStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get user account using write-through cache.
|
* Get user account using write-through cache.
|
||||||
|
* @param userAccount {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
public void writeThrough(UserAccount userAccount) {
|
public void writeThrough(final UserAccount userAccount) {
|
||||||
if (cache.contains(userAccount.getUserId())) {
|
if (cache.contains(userAccount.getUserId())) {
|
||||||
dbManager.updateDb(userAccount);
|
dbManager.updateDb(userAccount);
|
||||||
} else {
|
} else {
|
||||||
@ -84,11 +101,13 @@ public class CacheStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get user account using write-around cache.
|
* Get user account using write-around cache.
|
||||||
|
* @param userAccount {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
public void writeAround(UserAccount userAccount) {
|
public void writeAround(final UserAccount userAccount) {
|
||||||
if (cache.contains(userAccount.getUserId())) {
|
if (cache.contains(userAccount.getUserId())) {
|
||||||
dbManager.updateDb(userAccount);
|
dbManager.updateDb(userAccount);
|
||||||
cache.invalidate(userAccount.getUserId()); // Cache data has been updated -- remove older
|
// Cache data has been updated -- remove older
|
||||||
|
cache.invalidate(userAccount.getUserId());
|
||||||
// version from cache.
|
// version from cache.
|
||||||
} else {
|
} else {
|
||||||
dbManager.writeToDb(userAccount);
|
dbManager.writeToDb(userAccount);
|
||||||
@ -97,8 +116,10 @@ public class CacheStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get user account using read-through cache with write-back policy.
|
* Get user account using read-through cache with write-back policy.
|
||||||
|
* @param userId {@link String}
|
||||||
|
* @return {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
public UserAccount readThroughWithWriteBackPolicy(String userId) {
|
public UserAccount readThroughWithWriteBackPolicy(final String userId) {
|
||||||
if (cache.contains(userId)) {
|
if (cache.contains(userId)) {
|
||||||
LOGGER.info("# Found in cache!");
|
LOGGER.info("# Found in cache!");
|
||||||
return cache.get(userId);
|
return cache.get(userId);
|
||||||
@ -116,8 +137,9 @@ public class CacheStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set user account.
|
* Set user account.
|
||||||
|
* @param userAccount {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
public void writeBehind(UserAccount userAccount) {
|
public void writeBehind(final UserAccount userAccount) {
|
||||||
if (cache.isFull() && !cache.contains(userAccount.getUserId())) {
|
if (cache.isFull() && !cache.contains(userAccount.getUserId())) {
|
||||||
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
|
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
|
||||||
UserAccount toBeWrittenToDb = cache.getLruData();
|
UserAccount toBeWrittenToDb = cache.getLruData();
|
||||||
@ -148,6 +170,7 @@ public class CacheStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Print user accounts.
|
* Print user accounts.
|
||||||
|
* @return {@link String}
|
||||||
*/
|
*/
|
||||||
public String print() {
|
public String print() {
|
||||||
return Optional.ofNullable(cache)
|
return Optional.ofNullable(cache)
|
||||||
@ -160,22 +183,27 @@ public class CacheStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegate to backing cache store.
|
* Delegate to backing cache store.
|
||||||
|
* @param userId {@link String}
|
||||||
|
* @return {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
public UserAccount get(String userId) {
|
public UserAccount get(final String userId) {
|
||||||
return cache.get(userId);
|
return cache.get(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegate to backing cache store.
|
* Delegate to backing cache store.
|
||||||
|
* @param userId {@link String}
|
||||||
|
* @param userAccount {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
public void set(String userId, UserAccount userAccount) {
|
public void set(final String userId, final UserAccount userAccount) {
|
||||||
cache.set(userId, userAccount);
|
cache.set(userId, userAccount);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegate to backing cache store.
|
* Delegate to backing cache store.
|
||||||
|
* @param userId {@link String}
|
||||||
*/
|
*/
|
||||||
public void invalidate(String userId) {
|
public void invalidate(final String userId) {
|
||||||
cache.invalidate(userId);
|
cache.invalidate(userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user