2019-10-22 07:15:35 +02:00
|
|
|
/*
|
2016-01-27 22:20:42 +00:00
|
|
|
* The MIT License
|
2019-10-12 20:05:54 +03:00
|
|
|
* Copyright © 2014-2019 Ilkka Seppälä
|
2016-01-27 22:20:42 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-10-22 07:15:35 +02:00
|
|
|
|
2015-11-02 01:40:38 +08:00
|
|
|
package com.iluwatar.caching;
|
2015-10-28 23:55:47 +08:00
|
|
|
|
2019-11-10 00:53:12 +05:30
|
|
|
import java.util.List;
|
2019-11-13 01:26:46 +05:30
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.stream.Collectors;
|
2016-10-23 19:59:03 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-10-28 23:55:47 +08:00
|
|
|
/**
|
|
|
|
* The caching strategies are implemented in this class.
|
|
|
|
*/
|
|
|
|
public class CacheStore {
|
|
|
|
|
2016-10-23 19:59:03 +02:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(CacheStore.class);
|
|
|
|
|
2019-11-13 01:26:46 +05:30
|
|
|
private static LruCache cache;
|
2015-10-28 23:55:47 +08:00
|
|
|
|
2015-12-23 10:03:11 +05:00
|
|
|
private CacheStore() {
|
|
|
|
}
|
|
|
|
|
2015-12-25 23:49:28 +02:00
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Init cache capacity.
|
2015-12-25 23:49:28 +02:00
|
|
|
*/
|
2015-10-28 23:55:47 +08:00
|
|
|
public static void initCapacity(int capacity) {
|
2016-09-22 20:33:24 +02:00
|
|
|
if (cache == null) {
|
2015-12-25 23:49:28 +02:00
|
|
|
cache = new LruCache(capacity);
|
|
|
|
} else {
|
2015-10-28 23:55:47 +08:00
|
|
|
cache.setCapacity(capacity);
|
2015-12-25 23:49:28 +02:00
|
|
|
}
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 23:49:28 +02:00
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Get user account using read-through cache.
|
2015-12-25 23:49:28 +02:00
|
|
|
*/
|
|
|
|
public static UserAccount readThrough(String userId) {
|
|
|
|
if (cache.contains(userId)) {
|
2016-10-23 19:59:03 +02:00
|
|
|
LOGGER.info("# Cache Hit!");
|
2015-12-25 23:49:28 +02:00
|
|
|
return cache.get(userId);
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
2016-10-23 19:59:03 +02:00
|
|
|
LOGGER.info("# Cache Miss!");
|
2015-12-25 23:49:28 +02:00
|
|
|
UserAccount userAccount = DbManager.readFromDb(userId);
|
|
|
|
cache.set(userId, userAccount);
|
2015-10-28 23:55:47 +08:00
|
|
|
return userAccount;
|
|
|
|
}
|
|
|
|
|
2015-12-25 23:49:28 +02:00
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Get user account using write-through cache.
|
2015-12-25 23:49:28 +02:00
|
|
|
*/
|
2015-10-28 23:55:47 +08:00
|
|
|
public static void writeThrough(UserAccount userAccount) {
|
2015-12-25 23:49:28 +02:00
|
|
|
if (cache.contains(userAccount.getUserId())) {
|
|
|
|
DbManager.updateDb(userAccount);
|
2015-10-28 23:55:47 +08:00
|
|
|
} else {
|
2015-12-25 23:49:28 +02:00
|
|
|
DbManager.writeToDb(userAccount);
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
2015-12-25 23:49:28 +02:00
|
|
|
cache.set(userAccount.getUserId(), userAccount);
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 23:49:28 +02:00
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Get user account using write-around cache.
|
2015-12-25 23:49:28 +02:00
|
|
|
*/
|
2015-10-28 23:55:47 +08:00
|
|
|
public static void writeAround(UserAccount userAccount) {
|
2015-12-25 23:49:28 +02:00
|
|
|
if (cache.contains(userAccount.getUserId())) {
|
|
|
|
DbManager.updateDb(userAccount);
|
|
|
|
cache.invalidate(userAccount.getUserId()); // Cache data has been updated -- remove older
|
2019-11-10 00:53:12 +05:30
|
|
|
// version from cache.
|
2015-10-28 23:55:47 +08:00
|
|
|
} else {
|
2015-12-25 23:49:28 +02:00
|
|
|
DbManager.writeToDb(userAccount);
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 23:49:28 +02:00
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Get user account using read-through cache with write-back policy.
|
2015-12-25 23:49:28 +02:00
|
|
|
*/
|
|
|
|
public static UserAccount readThroughWithWriteBackPolicy(String userId) {
|
|
|
|
if (cache.contains(userId)) {
|
2016-10-23 19:59:03 +02:00
|
|
|
LOGGER.info("# Cache Hit!");
|
2015-12-25 23:49:28 +02:00
|
|
|
return cache.get(userId);
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
2016-10-23 19:59:03 +02:00
|
|
|
LOGGER.info("# Cache Miss!");
|
2015-12-25 23:49:28 +02:00
|
|
|
UserAccount userAccount = DbManager.readFromDb(userId);
|
2015-10-28 23:55:47 +08:00
|
|
|
if (cache.isFull()) {
|
2016-10-23 19:59:03 +02:00
|
|
|
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
|
2015-12-25 23:49:28 +02:00
|
|
|
UserAccount toBeWrittenToDb = cache.getLruData();
|
|
|
|
DbManager.upsertDb(toBeWrittenToDb);
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
2015-12-25 23:49:28 +02:00
|
|
|
cache.set(userId, userAccount);
|
2015-10-28 23:55:47 +08:00
|
|
|
return userAccount;
|
|
|
|
}
|
|
|
|
|
2015-12-25 23:49:28 +02:00
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Set user account.
|
2015-12-25 23:49:28 +02:00
|
|
|
*/
|
2015-10-28 23:55:47 +08:00
|
|
|
public static void writeBehind(UserAccount userAccount) {
|
2015-12-25 23:49:28 +02:00
|
|
|
if (cache.isFull() && !cache.contains(userAccount.getUserId())) {
|
2016-10-23 19:59:03 +02:00
|
|
|
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
|
2015-12-25 23:49:28 +02:00
|
|
|
UserAccount toBeWrittenToDb = cache.getLruData();
|
|
|
|
DbManager.upsertDb(toBeWrittenToDb);
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
2015-12-25 23:49:28 +02:00
|
|
|
cache.set(userAccount.getUserId(), userAccount);
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 23:49:28 +02:00
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Clears cache.
|
2015-12-25 23:49:28 +02:00
|
|
|
*/
|
2015-10-28 23:55:47 +08:00
|
|
|
public static void clearCache() {
|
2016-09-22 20:33:24 +02:00
|
|
|
if (cache != null) {
|
2015-10-28 23:55:47 +08:00
|
|
|
cache.clear();
|
2015-12-25 23:49:28 +02:00
|
|
|
}
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes remaining content in the cache into the DB.
|
|
|
|
*/
|
|
|
|
public static void flushCache() {
|
2016-10-23 19:59:03 +02:00
|
|
|
LOGGER.info("# flushCache...");
|
2019-11-13 01:26:46 +05:30
|
|
|
Optional.ofNullable(cache)
|
|
|
|
.map(LruCache::getCacheDataInListForm)
|
|
|
|
.orElse(List.of())
|
|
|
|
.forEach(DbManager::updateDb);
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 23:49:28 +02:00
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Print user accounts.
|
2015-12-25 23:49:28 +02:00
|
|
|
*/
|
2015-10-28 23:55:47 +08:00
|
|
|
public static String print() {
|
2019-11-13 01:26:46 +05:30
|
|
|
return Optional.ofNullable(cache)
|
|
|
|
.map(LruCache::getCacheDataInListForm)
|
|
|
|
.orElse(List.of())
|
|
|
|
.stream()
|
|
|
|
.map(userAccount -> userAccount.toString() + "\n")
|
|
|
|
.collect(Collectors.joining("", "\n--CACHE CONTENT--\n", "----\n"));
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|
2016-09-22 20:41:26 +02:00
|
|
|
|
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Delegate to backing cache store.
|
2016-09-22 20:41:26 +02:00
|
|
|
*/
|
|
|
|
public static UserAccount get(String userId) {
|
|
|
|
return cache.get(userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Delegate to backing cache store.
|
2016-09-22 20:41:26 +02:00
|
|
|
*/
|
|
|
|
public static void set(String userId, UserAccount userAccount) {
|
|
|
|
cache.set(userId, userAccount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-10 00:53:12 +05:30
|
|
|
* Delegate to backing cache store.
|
2016-09-22 20:41:26 +02:00
|
|
|
*/
|
|
|
|
public static void invalidate(String userId) {
|
|
|
|
cache.invalidate(userId);
|
|
|
|
}
|
2015-10-28 23:55:47 +08:00
|
|
|
}
|