Issue #273:Caching Patterns [new pattern]
This commit is contained in:
parent
a869294851
commit
9891c2e17b
1
caching/.gitignore
vendored
Normal file
1
caching/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target/
|
BIN
caching/etc/caching.png
Normal file
BIN
caching/etc/caching.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 50 KiB |
106
caching/etc/caching.ucls
Normal file
106
caching/etc/caching.ucls
Normal file
@ -0,0 +1,106 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<class id="1" language="java" name="main.java.com.wssia.caching.App" project="CachingPatterns"
|
||||
file="/CachingPatterns/src/main/java/com/wssia/caching/App.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="249" y="150"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="2" language="java" name="main.java.com.wssia.caching.AppManager" project="CachingPatterns"
|
||||
file="/CachingPatterns/src/main/java/com/wssia/caching/AppManager.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="502" y="163"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="3" language="java" name="main.java.com.wssia.caching.CacheStore" project="CachingPatterns"
|
||||
file="/CachingPatterns/src/main/java/com/wssia/caching/CacheStore.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="537" y="436"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<enumeration id="4" language="java" name="main.java.com.wssia.caching.CachingPolicy" project="CachingPatterns"
|
||||
file="/CachingPatterns/src/main/java/com/wssia/caching/CachingPolicy.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="789" y="162"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<class id="5" language="java" name="main.java.com.wssia.caching.DBManager" project="CachingPatterns"
|
||||
file="/CachingPatterns/src/main/java/com/wssia/caching/DBManager.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="1137" y="134"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="6" language="java" name="main.java.com.wssia.caching.LRUCache" project="CachingPatterns"
|
||||
file="/CachingPatterns/src/main/java/com/wssia/caching/LRUCache.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="884" y="435"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="7" language="java" name="main.java.com.wssia.caching.UserAccount" project="CachingPatterns"
|
||||
file="/CachingPatterns/src/main/java/com/wssia/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="1137" y="382"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="8" language="java" name="test.java.com.wssia.caching.AppTest" project="CachingPatterns"
|
||||
file="/CachingPatterns/src/test/java/com/wssia/caching/AppTest.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="251" y="374"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<association id="9">
|
||||
<end type="SOURCE" refId="2" navigable="false">
|
||||
<attribute id="10" name="cachingPolicy"/>
|
||||
<multiplicity id="11" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="4" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="12">
|
||||
<end type="SOURCE" refId="8" navigable="false">
|
||||
<attribute id="13" name="app"/>
|
||||
<multiplicity id="14" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="15">
|
||||
<end type="SOURCE" refId="3" navigable="false">
|
||||
<attribute id="16" name="cache"/>
|
||||
<multiplicity id="17" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="6" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
24
caching/index.md
Normal file
24
caching/index.md
Normal file
@ -0,0 +1,24 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Caching
|
||||
folder: caching
|
||||
permalink: /patterns/caching/
|
||||
categories: Other
|
||||
tags:
|
||||
- Java
|
||||
---
|
||||
|
||||
**Intent:** To avoid expensive re-acquisition of resources by not releasing
|
||||
the resources immediately after their use. The resources retain their identity, are kept in some
|
||||
fast-access storage, and are re-used to avoid having to acquire them again.
|
||||
|
||||

|
||||
|
||||
**Applicability:** Use the Caching pattern(s) when
|
||||
|
||||
* Repetitious acquisition, initialization, and release of the same resource causes unnecessary performance overhead.
|
||||
|
||||
**Credits**
|
||||
|
||||
* [Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
|
||||
* [Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
|
51
caching/pom.xml
Normal file
51
caching/pom.xml
Normal file
@ -0,0 +1,51 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.7.0</version>
|
||||
</parent>
|
||||
<artifactId>caching</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver</artifactId>
|
||||
<version>3.0.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-core</artifactId>
|
||||
<version>3.0.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>bson</artifactId>
|
||||
<version>3.0.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<!--
|
||||
Due to the use of MongoDB in the test of this pattern, TRAVIS and/or MAVEN might fail if the DB connection is
|
||||
not open for the JUnit test. To avoid disrupting the compilation process, the surefire plug-in was used
|
||||
to SKIP the running of the JUnit tests for this pattern. To RE-ACTIVATE the running of the tests, change the
|
||||
skipTests (below) flag to 'false'.
|
||||
-->
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.19</version>
|
||||
<configuration>
|
||||
<skipTests>true</skipTests>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
100
caching/src/main/java/com/wssia/caching/App.java
Normal file
100
caching/src/main/java/com/wssia/caching/App.java
Normal file
@ -0,0 +1,100 @@
|
||||
package main.java.com.wssia.caching;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Caching pattern describes how to avoid expensive re-acquisition of resources by not releasing
|
||||
* the resources immediately after their use. The resources retain their identity, are kept in some
|
||||
* fast-access storage, and are re-used to avoid having to acquire them again. There are three main
|
||||
* caching strategies/techniques in this pattern; each with their own pros and cons. They are:
|
||||
* <code>write-through</code> which writes data to the cache and DB in a single transaction,
|
||||
* <code>write-around</code> which writes data immediately into the DB instead of the cache, and
|
||||
* <code>write-behind</code> which writes data into the cache initially whilst the data is only
|
||||
* written into the DB when the cache is full. The <code>read-through</code> strategy is also
|
||||
* included in the mentioned three strategies -- returns data from the cache to the caller <b>if</b>
|
||||
* it exists <b>else</b> queries from DB and stores it into the cache for future use. These
|
||||
* strategies determine when the data in the cache should be written back to the backing store (i.e.
|
||||
* Database) and help keep both data sources synchronized/up-to-date. This pattern can improve
|
||||
* performance and also helps to maintain consistency between data held in the cache and the data in
|
||||
* the underlying data store.
|
||||
* <p>
|
||||
* In this example, the user account ({@link UserAccount}) entity is used as the underlying
|
||||
* application data. The cache itself is implemented as an internal (Java) data structure. It adopts
|
||||
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The three
|
||||
* strategies are individually tested. The testing of the cache is restricted towards saving and
|
||||
* querying of user accounts from the underlying data store ( {@link DBManager}). The main class (
|
||||
* {@link App} is not aware of the underlying mechanics of the application (i.e. save and query) and
|
||||
* whether the data is coming from the cache or the DB (i.e. separation of concern). The AppManager
|
||||
* ({@link AppManager}) handles the transaction of data to-and-from the underlying data store
|
||||
* (depending on the preferred caching policy/strategy).
|
||||
*
|
||||
* <i>App --> AppManager --> CacheStore/LRUCache/CachingPolicy --> DBManager</i>
|
||||
* <p>
|
||||
*
|
||||
* @see CacheStore
|
||||
* @See LRUCache
|
||||
* @see CachingPolicy
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Read-through and write-through
|
||||
*/
|
||||
public void useReadAndWriteThroughStrategy() {
|
||||
System.out.println("# CachingPolicy.THROUGH");
|
||||
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
|
||||
|
||||
UserAccount userAccount1 = new UserAccount("001", "John", "He is a boy.");
|
||||
|
||||
AppManager.save(userAccount1);
|
||||
System.out.println(AppManager.printCacheContent());
|
||||
userAccount1 = AppManager.find("001");
|
||||
userAccount1 = AppManager.find("001");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read-through and write-around
|
||||
*/
|
||||
public void useReadThroughAndWriteAroundStrategy() {
|
||||
System.out.println("# CachingPolicy.AROUND");
|
||||
AppManager.initCachingPolicy(CachingPolicy.AROUND);
|
||||
|
||||
UserAccount userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
|
||||
|
||||
AppManager.save(userAccount2);
|
||||
System.out.println(AppManager.printCacheContent());
|
||||
userAccount2 = AppManager.find("002");
|
||||
System.out.println(AppManager.printCacheContent());
|
||||
userAccount2 = AppManager.find("002");
|
||||
userAccount2.setUserName("Jane G.");
|
||||
AppManager.save(userAccount2);
|
||||
System.out.println(AppManager.printCacheContent());
|
||||
userAccount2 = AppManager.find("002");
|
||||
System.out.println(AppManager.printCacheContent());
|
||||
userAccount2 = AppManager.find("002");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read-through and write-behind
|
||||
*/
|
||||
public void useReadThroughAndWriteBehindStrategy() {
|
||||
System.out.println("# CachingPolicy.BEHIND");
|
||||
AppManager.initCachingPolicy(CachingPolicy.BEHIND);
|
||||
|
||||
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
||||
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
||||
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
||||
|
||||
AppManager.save(userAccount3);
|
||||
AppManager.save(userAccount4);
|
||||
AppManager.save(userAccount5);
|
||||
System.out.println(AppManager.printCacheContent());
|
||||
userAccount3 = AppManager.find("003");
|
||||
System.out.println(AppManager.printCacheContent());
|
||||
UserAccount userAccount6 = new UserAccount("006", "Yasha", "She is an only child.");
|
||||
AppManager.save(userAccount6);
|
||||
System.out.println(AppManager.printCacheContent());
|
||||
userAccount4 = AppManager.find("004");
|
||||
System.out.println(AppManager.printCacheContent());
|
||||
}
|
||||
}
|
65
caching/src/main/java/com/wssia/caching/AppManager.java
Normal file
65
caching/src/main/java/com/wssia/caching/AppManager.java
Normal file
@ -0,0 +1,65 @@
|
||||
package main.java.com.wssia.caching;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
/**
|
||||
*
|
||||
* AppManager helps to bridge the gap in communication between the main class and the application's
|
||||
* back-end. DB connection is initialized through this class. The chosen caching strategy/policy is
|
||||
* also initialized here. Before the cache can be used, the size of the cache has to be set.
|
||||
* Depending on the chosen caching policy, AppManager will call the appropriate function in the
|
||||
* CacheStore class.
|
||||
*
|
||||
*/
|
||||
public class AppManager {
|
||||
|
||||
private static CachingPolicy cachingPolicy;
|
||||
|
||||
public static void init() {
|
||||
try {
|
||||
DBManager.connect();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void initCachingPolicy(CachingPolicy policy) {
|
||||
cachingPolicy = policy;
|
||||
if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CacheStore.flushCache();
|
||||
}
|
||||
}));
|
||||
}
|
||||
CacheStore.clearCache();
|
||||
}
|
||||
|
||||
public static void initCacheCapacity(int capacity) {
|
||||
CacheStore.initCapacity(capacity);
|
||||
}
|
||||
|
||||
public static UserAccount find(String userID) {
|
||||
if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) {
|
||||
return CacheStore.readThrough(userID);
|
||||
} else if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
return CacheStore.readThroughWithWriteBackPolicy(userID);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void save(UserAccount userAccount) {
|
||||
if (cachingPolicy == CachingPolicy.THROUGH) {
|
||||
CacheStore.writeThrough(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.AROUND) {
|
||||
CacheStore.writeAround(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
CacheStore.writeBehind(userAccount);
|
||||
}
|
||||
}
|
||||
|
||||
public static String printCacheContent() {
|
||||
return CacheStore.print();
|
||||
}
|
||||
}
|
104
caching/src/main/java/com/wssia/caching/CacheStore.java
Normal file
104
caching/src/main/java/com/wssia/caching/CacheStore.java
Normal file
@ -0,0 +1,104 @@
|
||||
package main.java.com.wssia.caching;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* The caching strategies are implemented in this class.
|
||||
*
|
||||
*/
|
||||
public class CacheStore {
|
||||
|
||||
static LRUCache cache = null;
|
||||
|
||||
public static void initCapacity(int capacity) {
|
||||
if (null == cache)
|
||||
cache = new LRUCache(capacity);
|
||||
else
|
||||
cache.setCapacity(capacity);
|
||||
}
|
||||
|
||||
public static UserAccount readThrough(String userID) {
|
||||
if (cache.contains(userID)) {
|
||||
System.out.println("# Cache Hit!");
|
||||
return cache.get(userID);
|
||||
}
|
||||
System.out.println("# Cache Miss!");
|
||||
UserAccount userAccount = DBManager.readFromDB(userID);
|
||||
cache.set(userID, userAccount);
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
public static void writeThrough(UserAccount userAccount) {
|
||||
if (cache.contains(userAccount.getUserID())) {
|
||||
DBManager.updateDB(userAccount);
|
||||
} else {
|
||||
DBManager.writeToDB(userAccount);
|
||||
}
|
||||
cache.set(userAccount.getUserID(), userAccount);
|
||||
}
|
||||
|
||||
public static void writeAround(UserAccount userAccount) {
|
||||
if (cache.contains(userAccount.getUserID())) {
|
||||
DBManager.updateDB(userAccount);
|
||||
cache.invalidate(userAccount.getUserID()); // Cache data has been updated -- remove older
|
||||
// version from cache.
|
||||
} else {
|
||||
DBManager.writeToDB(userAccount);
|
||||
}
|
||||
}
|
||||
|
||||
public static UserAccount readThroughWithWriteBackPolicy(String userID) {
|
||||
if (cache.contains(userID)) {
|
||||
System.out.println("# Cache Hit!");
|
||||
return cache.get(userID);
|
||||
}
|
||||
System.out.println("# Cache Miss!");
|
||||
UserAccount userAccount = DBManager.readFromDB(userID);
|
||||
if (cache.isFull()) {
|
||||
System.out.println("# Cache is FULL! Writing LRU data to DB...");
|
||||
UserAccount toBeWrittenToDB = cache.getLRUData();
|
||||
DBManager.upsertDB(toBeWrittenToDB);
|
||||
}
|
||||
cache.set(userID, userAccount);
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
public static void writeBehind(UserAccount userAccount) {
|
||||
if (cache.isFull() && !cache.contains(userAccount.getUserID())) {
|
||||
System.out.println("# Cache is FULL! Writing LRU data to DB...");
|
||||
UserAccount toBeWrittenToDB = cache.getLRUData();
|
||||
DBManager.upsertDB(toBeWrittenToDB);
|
||||
}
|
||||
cache.set(userAccount.getUserID(), userAccount);
|
||||
}
|
||||
|
||||
public static void clearCache() {
|
||||
if (null != cache)
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes remaining content in the cache into the DB.
|
||||
*/
|
||||
public static void flushCache() {
|
||||
System.out.println("# flushCache...");
|
||||
if (null == cache)
|
||||
return;
|
||||
ArrayList<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
|
||||
for (UserAccount userAccount : listOfUserAccounts) {
|
||||
DBManager.upsertDB(userAccount);
|
||||
}
|
||||
}
|
||||
|
||||
public static String print() {
|
||||
ArrayList<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("\n--CACHE CONTENT--\n");
|
||||
for (UserAccount userAccount : listOfUserAccounts) {
|
||||
sb.append(userAccount.toString() + "\n");
|
||||
}
|
||||
sb.append("----\n");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
20
caching/src/main/java/com/wssia/caching/CachingPolicy.java
Normal file
20
caching/src/main/java/com/wssia/caching/CachingPolicy.java
Normal file
@ -0,0 +1,20 @@
|
||||
package main.java.com.wssia.caching;
|
||||
|
||||
/**
|
||||
*
|
||||
* Enum class containing the three caching strategies implemented in the pattern.
|
||||
*
|
||||
*/
|
||||
public enum CachingPolicy {
|
||||
THROUGH("through"), AROUND("around"), BEHIND("behind");
|
||||
|
||||
private String policy;
|
||||
|
||||
private CachingPolicy(String policy) {
|
||||
this.policy = policy;
|
||||
}
|
||||
|
||||
public String getPolicy() {
|
||||
return policy;
|
||||
}
|
||||
}
|
92
caching/src/main/java/com/wssia/caching/DBManager.java
Normal file
92
caching/src/main/java/com/wssia/caching/DBManager.java
Normal file
@ -0,0 +1,92 @@
|
||||
package main.java.com.wssia.caching;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
import org.bson.Document;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.client.model.UpdateOptions;
|
||||
|
||||
/**
|
||||
*
|
||||
* DBManager handles the communication with the underlying data store i.e. Database. It contains the
|
||||
* implemented methods for querying, inserting, and updating data. MongoDB was used as the database
|
||||
* for the application.
|
||||
*
|
||||
*/
|
||||
public class DBManager {
|
||||
|
||||
private static MongoClient mongoClient;
|
||||
private static MongoDatabase db;
|
||||
|
||||
public static void connect() throws ParseException {
|
||||
mongoClient = new MongoClient();
|
||||
db = mongoClient.getDatabase("test");
|
||||
}
|
||||
|
||||
public static UserAccount readFromDB(String userID) {
|
||||
if (null == db) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
FindIterable<Document> iterable =
|
||||
db.getCollection("user_accounts").find(new Document("userID", userID));
|
||||
if (iterable == null)
|
||||
return null;
|
||||
Document doc = iterable.first();
|
||||
UserAccount userAccount =
|
||||
new UserAccount(userID, doc.getString("userName"), doc.getString("additionalInfo"));
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
public static void writeToDB(UserAccount userAccount) {
|
||||
if (null == db) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
db.getCollection("user_accounts").insertOne(
|
||||
new Document("userID", userAccount.getUserID()).append("userName",
|
||||
userAccount.getUserName()).append("additionalInfo", userAccount.getAdditionalInfo()));
|
||||
}
|
||||
|
||||
public static void updateDB(UserAccount userAccount) {
|
||||
if (null == db) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
db.getCollection("user_accounts").updateOne(
|
||||
new Document("userID", userAccount.getUserID()),
|
||||
new Document("$set", new Document("userName", userAccount.getUserName()).append(
|
||||
"additionalInfo", userAccount.getAdditionalInfo())));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Insert data into DB if it does not exist. Else, update it.
|
||||
*/
|
||||
public static void upsertDB(UserAccount userAccount) {
|
||||
if (null == db) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
db.getCollection("user_accounts").updateOne(
|
||||
new Document("userID", userAccount.getUserID()),
|
||||
new Document("$set", new Document("userID", userAccount.getUserID()).append("userName",
|
||||
userAccount.getUserName()).append("additionalInfo", userAccount.getAdditionalInfo())),
|
||||
new UpdateOptions().upsert(true));
|
||||
}
|
||||
}
|
146
caching/src/main/java/com/wssia/caching/LRUCache.java
Normal file
146
caching/src/main/java/com/wssia/caching/LRUCache.java
Normal file
@ -0,0 +1,146 @@
|
||||
package main.java.com.wssia.caching;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
*
|
||||
* Data structure/implementation of the application's cache. The data structure consists of a hash
|
||||
* table attached with a doubly linked-list. The linked-list helps in capturing and maintaining the
|
||||
* LRU data in the cache. When a data is queried (from the cache), added (to the cache), or updated,
|
||||
* the data is moved to the front of the list to depict itself as the most-recently-used data. The
|
||||
* LRU data is always at the end of the list.
|
||||
*
|
||||
*/
|
||||
public class LRUCache {
|
||||
|
||||
class Node {
|
||||
String userID;
|
||||
UserAccount userAccount;
|
||||
Node previous;
|
||||
Node next;
|
||||
|
||||
public Node(String userID, UserAccount userAccount) {
|
||||
this.userID = userID;
|
||||
this.userAccount = userAccount;
|
||||
}
|
||||
}
|
||||
|
||||
int capacity;
|
||||
HashMap<String, Node> cache = new HashMap<String, Node>();
|
||||
Node head = null;
|
||||
Node end = null;
|
||||
|
||||
public LRUCache(int capacity) {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public UserAccount get(String userID) {
|
||||
if (cache.containsKey(userID)) {
|
||||
Node node = cache.get(userID);
|
||||
remove(node);
|
||||
setHead(node);
|
||||
return node.userAccount;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Remove node from linked list.
|
||||
*/
|
||||
public void remove(Node node) {
|
||||
if (node.previous != null) {
|
||||
node.previous.next = node.next;
|
||||
} else {
|
||||
head = node.next;
|
||||
}
|
||||
if (node.next != null) {
|
||||
node.next.previous = node.previous;
|
||||
} else {
|
||||
end = node.previous;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Move node to the front of the list.
|
||||
*/
|
||||
public void setHead(Node node) {
|
||||
node.next = head;
|
||||
node.previous = null;
|
||||
if (head != null)
|
||||
head.previous = node;
|
||||
head = node;
|
||||
if (end == null)
|
||||
end = head;
|
||||
}
|
||||
|
||||
public void set(String userID, UserAccount userAccount) {
|
||||
if (cache.containsKey(userID)) {
|
||||
Node old = cache.get(userID);
|
||||
old.userAccount = userAccount;
|
||||
remove(old);
|
||||
setHead(old);
|
||||
} else {
|
||||
Node newNode = new Node(userID, userAccount);
|
||||
if (cache.size() >= capacity) {
|
||||
System.out.println("# Cache is FULL! Removing " + end.userID + " from cache...");
|
||||
cache.remove(end.userID); // remove LRU data from cache.
|
||||
remove(end);
|
||||
setHead(newNode);
|
||||
} else {
|
||||
setHead(newNode);
|
||||
}
|
||||
cache.put(userID, newNode);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean contains(String userID) {
|
||||
return cache.containsKey(userID);
|
||||
}
|
||||
|
||||
public void invalidate(String userID) {
|
||||
System.out.println("# " + userID + " has been updated! Removing older version from cache...");
|
||||
Node toBeRemoved = cache.get(userID);
|
||||
remove(toBeRemoved);
|
||||
cache.remove(userID);
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
return cache.size() >= capacity;
|
||||
}
|
||||
|
||||
public UserAccount getLRUData() {
|
||||
return end.userAccount;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = null;
|
||||
end = null;
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Returns cache data in list form.
|
||||
*/
|
||||
public ArrayList<UserAccount> getCacheDataInListForm() {
|
||||
ArrayList<UserAccount> listOfCacheData = new ArrayList<UserAccount>();
|
||||
Node temp = head;
|
||||
while (temp != null) {
|
||||
listOfCacheData.add(temp.userAccount);
|
||||
temp = temp.next;
|
||||
}
|
||||
return listOfCacheData;
|
||||
}
|
||||
|
||||
public void setCapacity(int newCapacity) {
|
||||
if (capacity > newCapacity) {
|
||||
clear(); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll
|
||||
// just clear the cache.
|
||||
} else {
|
||||
this.capacity = newCapacity;
|
||||
}
|
||||
}
|
||||
}
|
47
caching/src/main/java/com/wssia/caching/UserAccount.java
Normal file
47
caching/src/main/java/com/wssia/caching/UserAccount.java
Normal file
@ -0,0 +1,47 @@
|
||||
package main.java.com.wssia.caching;
|
||||
|
||||
/**
|
||||
*
|
||||
* Entity class (stored in cache and DB) used in the application.
|
||||
*
|
||||
*/
|
||||
public class UserAccount {
|
||||
private String userID;
|
||||
private String userName;
|
||||
private String additionalInfo;
|
||||
|
||||
public UserAccount(String userID, String userName, String additionalInfo) {
|
||||
this.userID = userID;
|
||||
this.userName = userName;
|
||||
this.additionalInfo = additionalInfo;
|
||||
}
|
||||
|
||||
public String getUserID() {
|
||||
return userID;
|
||||
}
|
||||
|
||||
public void setUserID(String userID) {
|
||||
this.userID = userID;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getAdditionalInfo() {
|
||||
return additionalInfo;
|
||||
}
|
||||
|
||||
public void setAdditionalInfo(String additionalInfo) {
|
||||
this.additionalInfo = additionalInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return userID + ", " + userName + ", " + additionalInfo;
|
||||
}
|
||||
}
|
41
caching/src/test/java/com/wssia/caching/AppTest.java
Normal file
41
caching/src/test/java/com/wssia/caching/AppTest.java
Normal file
@ -0,0 +1,41 @@
|
||||
package test.java.com.wssia.caching;
|
||||
|
||||
import main.java.com.wssia.caching.App;
|
||||
import main.java.com.wssia.caching.AppManager;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
App app;
|
||||
|
||||
/**
|
||||
* Setup of application test includes: initializing DB connection and cache size/capacity.
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
AppManager.init();
|
||||
AppManager.initCacheCapacity(3);
|
||||
app = new App();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadAndWriteThroughStrategy() {
|
||||
app.useReadAndWriteThroughStrategy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadThroughAndWriteAroundStrategy() {
|
||||
app.useReadThroughAndWriteAroundStrategy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadThroughAndWriteBehindStrategy() {
|
||||
app.useReadThroughAndWriteBehindStrategy();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user