Fix CacheStore errors from checkstyle plugin 107 left
This commit is contained in:
@ -35,20 +35,34 @@ import lombok.extern.slf4j.Slf4j;
|
||||
*/
|
||||
@Slf4j
|
||||
public class CacheStore {
|
||||
/**
|
||||
* Cach capacity.
|
||||
*/
|
||||
private static final int CAPACITY = 3;
|
||||
|
||||
/**
|
||||
* Lru cache see {@link LruCache}.
|
||||
*/
|
||||
private static LruCache cache;
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Init cache capacity.
|
||||
* @param capacity int
|
||||
*/
|
||||
public void initCapacity(int capacity) {
|
||||
public void initCapacity(final int capacity) {
|
||||
if (cache == null) {
|
||||
cache = new LruCache(capacity);
|
||||
} else {
|
||||
@ -58,8 +72,10 @@ public class CacheStore {
|
||||
|
||||
/**
|
||||
* 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)) {
|
||||
LOGGER.info("# Found in Cache!");
|
||||
return cache.get(userId);
|
||||
@ -72,8 +88,9 @@ public class CacheStore {
|
||||
|
||||
/**
|
||||
* 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())) {
|
||||
dbManager.updateDb(userAccount);
|
||||
} else {
|
||||
@ -84,11 +101,13 @@ public class CacheStore {
|
||||
|
||||
/**
|
||||
* 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())) {
|
||||
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.
|
||||
} else {
|
||||
dbManager.writeToDb(userAccount);
|
||||
@ -97,8 +116,10 @@ public class CacheStore {
|
||||
|
||||
/**
|
||||
* 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)) {
|
||||
LOGGER.info("# Found in cache!");
|
||||
return cache.get(userId);
|
||||
@ -116,8 +137,9 @@ public class CacheStore {
|
||||
|
||||
/**
|
||||
* 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())) {
|
||||
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
|
||||
UserAccount toBeWrittenToDb = cache.getLruData();
|
||||
@ -148,6 +170,7 @@ public class CacheStore {
|
||||
|
||||
/**
|
||||
* Print user accounts.
|
||||
* @return {@link String}
|
||||
*/
|
||||
public String print() {
|
||||
return Optional.ofNullable(cache)
|
||||
@ -160,22 +183,27 @@ public class CacheStore {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate to backing cache store.
|
||||
* @param userId {@link String}
|
||||
*/
|
||||
public void invalidate(String userId) {
|
||||
public void invalidate(final String userId) {
|
||||
cache.invalidate(userId);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user