Caching pattern: Documentation and diagram
This commit is contained in:
@ -27,3 +27,4 @@ Use the Caching pattern(s) when
|
|||||||
|
|
||||||
* [Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
|
* [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)
|
* [Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
|
||||||
|
* [Cache-Aside](https://msdn.microsoft.com/en-us/library/dn589799.aspx)
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 112 KiB |
@ -1,106 +1,90 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
<class-diagram version="1.1.11" icons="true" automaticImage="PNG" always-add-relationships="false"
|
||||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
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"
|
router="FAN">
|
||||||
file="/CachingPatterns/src/main/java/com/wssia/caching/App.java" binary="false" corner="BOTTOM_RIGHT">
|
<class id="1" language="java" name="com.iluwatar.caching.CacheStore" project="caching"
|
||||||
<position height="-1" width="-1" x="249" y="150"/>
|
file="/caching/src/main/java/com/iluwatar/caching/CacheStore.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<position height="-1" width="-1" x="322" y="444"/>
|
||||||
sort-features="false" accessors="true" visibility="true">
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
sort-features="false" accessors="true" visibility="true">
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
</display>
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</class>
|
||||||
|
<class id="2" language="java" name="com.iluwatar.caching.LruCache" project="caching"
|
||||||
|
file="/caching/src/main/java/com/iluwatar/caching/LruCache.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="700" y="446"/>
|
||||||
|
<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="3" language="java" name="com.iluwatar.caching.CachingPolicy" project="caching"
|
||||||
|
file="/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="943" y="122"/>
|
||||||
|
<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="4" language="java" name="com.iluwatar.caching.DbManager" project="caching"
|
||||||
|
file="/caching/src/main/java/com/iluwatar/caching/DbManager.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="1011" y="426"/>
|
||||||
|
<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="5" language="java" name="com.iluwatar.caching.App" project="caching"
|
||||||
|
file="/caching/src/main/java/com/iluwatar/caching/App.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="304" y="95"/>
|
||||||
|
<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="com.iluwatar.caching.AppManager" project="caching"
|
||||||
|
file="/caching/src/main/java/com/iluwatar/caching/AppManager.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="604" y="122"/>
|
||||||
|
<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="com.iluwatar.caching.UserAccount" project="caching"
|
||||||
|
file="/caching/src/main/java/com/iluwatar/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="1161" y="139"/>
|
||||||
|
<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>
|
||||||
<class id="2" language="java" name="main.java.com.wssia.caching.AppManager" project="CachingPatterns"
|
<association id="8">
|
||||||
file="/CachingPatterns/src/main/java/com/wssia/caching/AppManager.java" binary="false" corner="BOTTOM_RIGHT">
|
<end type="SOURCE" refId="1" navigable="false">
|
||||||
<position height="-1" width="-1" x="502" y="163"/>
|
<attribute id="9" name="cache"/>
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<multiplicity id="10" minimum="0" maximum="1"/>
|
||||||
sort-features="false" accessors="true" visibility="true">
|
</end>
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
<end type="TARGET" refId="2" navigable="true"/>
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
<display labels="true" multiplicity="true"/>
|
||||||
</display>
|
</association>
|
||||||
</class>
|
<association id="11">
|
||||||
<class id="3" language="java" name="main.java.com.wssia.caching.CacheStore" project="CachingPatterns"
|
<end type="SOURCE" refId="6" navigable="false">
|
||||||
file="/CachingPatterns/src/main/java/com/wssia/caching/CacheStore.java" binary="false" corner="BOTTOM_RIGHT">
|
<attribute id="12" name="cachingPolicy"/>
|
||||||
<position height="-1" width="-1" x="537" y="436"/>
|
<multiplicity id="13" minimum="0" maximum="1"/>
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
</end>
|
||||||
sort-features="false" accessors="true" visibility="true">
|
<end type="TARGET" refId="3" navigable="true"/>
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
<display labels="true" multiplicity="true"/>
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
</association>
|
||||||
</display>
|
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
</class>
|
sort-features="false" accessors="true" visibility="true">
|
||||||
<enumeration id="4" language="java" name="main.java.com.wssia.caching.CachingPolicy" project="CachingPatterns"
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
file="/CachingPatterns/src/main/java/com/wssia/caching/CachingPolicy.java" binary="false" corner="BOTTOM_RIGHT">
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
<position height="-1" width="-1" x="789" y="162"/>
|
</classifier-display>
|
||||||
<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="1140" y="405"/>
|
|
||||||
<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"/>
|
<association-display labels="true" multiplicity="true"/>
|
||||||
</class-diagram>
|
</class-diagram>
|
||||||
|
@ -26,22 +26,23 @@ package com.iluwatar.caching;
|
|||||||
*
|
*
|
||||||
* The Caching pattern describes how to avoid expensive re-acquisition of resources by not releasing
|
* 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
|
* 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
|
* fast-access storage, and are re-used to avoid having to acquire them again. There are four main
|
||||||
* caching strategies/techniques in this pattern; each with their own pros and cons. They are:
|
* 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-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-around</code> which writes data immediately into the DB instead of the cache,
|
||||||
* <code>write-behind</code> which writes data into the cache initially whilst the data is only
|
* <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
|
* written into the DB when the cache is full, and <code>cache-aside</code> which pushes the
|
||||||
* included in the mentioned three strategies -- returns data from the cache to the caller <b>if</b>
|
* responsibility of keeping the data synchronized in both data sources to the application itself.
|
||||||
* it exists <b>else</b> queries from DB and stores it into the cache for future use. These
|
* The <code>read-through</code> strategy is also included in the mentioned four strategies --
|
||||||
* strategies determine when the data in the cache should be written back to the backing store (i.e.
|
* returns data from the cache to the caller <b>if</b> it exists <b>else</b> queries from DB and
|
||||||
* Database) and help keep both data sources synchronized/up-to-date. This pattern can improve
|
* stores it into the cache for future use. These strategies determine when the data in the cache
|
||||||
* performance and also helps to maintain consistency between data held in the cache and the data in
|
* should be written back to the backing store (i.e. Database) and help keep both data sources
|
||||||
* the underlying data store.
|
* 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>
|
* <p>
|
||||||
* In this example, the user account ({@link UserAccount}) entity is used as the underlying
|
* 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
|
* 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
|
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The four
|
||||||
* strategies are individually tested. The testing of the cache is restricted towards saving and
|
* 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 (
|
* 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
|
* {@link App} is not aware of the underlying mechanics of the application (i.e. save and query) and
|
||||||
|
@ -24,7 +24,7 @@ package com.iluwatar.caching;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Enum class containing the three caching strategies implemented in the pattern.
|
* Enum class containing the four caching strategies implemented in the pattern.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public enum CachingPolicy {
|
public enum CachingPolicy {
|
||||||
|
@ -73,7 +73,6 @@ public class LruCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Remove node from linked list.
|
* Remove node from linked list.
|
||||||
*/
|
*/
|
||||||
public void remove(Node node) {
|
public void remove(Node node) {
|
||||||
@ -90,7 +89,6 @@ public class LruCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Move node to the front of the list.
|
* Move node to the front of the list.
|
||||||
*/
|
*/
|
||||||
public void setHead(Node node) {
|
public void setHead(Node node) {
|
||||||
@ -161,7 +159,6 @@ public class LruCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Returns cache data in list form.
|
* Returns cache data in list form.
|
||||||
*/
|
*/
|
||||||
public List<UserAccount> getCacheDataInListForm() {
|
public List<UserAccount> getCacheDataInListForm() {
|
||||||
|
@ -23,9 +23,7 @@
|
|||||||
package com.iluwatar.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Entity class (stored in cache and DB) used in the application.
|
* Entity class (stored in cache and DB) used in the application.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class UserAccount {
|
public class UserAccount {
|
||||||
private String userId;
|
private String userId;
|
||||||
|
Reference in New Issue
Block a user