Skip to content

Commit 479cc07

Browse files
committed
put cache example for eclipse and maven
0 parents  commit 479cc07

16 files changed

+434
-0
lines changed

.classpath

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="src-test"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
7+
<classpathentry kind="output" path="bin"/>
8+
</classpath>

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>cache-java</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8

demo.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java -jar ./target/j262-cache.jar

pom.xml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.j262.cache</groupId>
8+
<version>0.0.1</version>
9+
<artifactId>j262-cache</artifactId>
10+
<packaging>jar</packaging>
11+
12+
<dependencies>
13+
<dependency>
14+
<groupId>junit</groupId>
15+
<artifactId>junit</artifactId>
16+
<version>4.11</version>
17+
<scope>test</scope>
18+
</dependency>
19+
</dependencies>
20+
21+
22+
<build>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-jar-plugin</artifactId>
27+
<configuration>
28+
<archive>
29+
<manifest>
30+
<addClasspath>true</addClasspath>
31+
<mainClass>org.j262.cache.demo.CacheDemo</mainClass>
32+
</manifest>
33+
</archive>
34+
<finalName>j262-cache</finalName>
35+
</configuration>
36+
<version>2.6</version>
37+
</plugin>
38+
</plugins>
39+
<sourceDirectory>src</sourceDirectory>
40+
<testSourceDirectory>src-test</testSourceDirectory>
41+
</build>
42+
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.j262.cache.test;
2+
3+
import org.junit.runner.RunWith;
4+
import org.junit.runners.Suite;
5+
import org.junit.runners.Suite.SuiteClasses;
6+
7+
@RunWith(Suite.class)
8+
@SuiteClasses({ CacheTest.class })
9+
10+
public class AllCacheJavaTests {
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.j262.cache.test;
2+
3+
import junit.framework.TestCase;
4+
5+
import org.j262.cache.Cache;
6+
import org.j262.cache.Cache.CacheType;
7+
import org.j262.cache.exception.CacheException;
8+
import org.junit.Test;
9+
10+
/**
11+
* @author batsura-sa
12+
*/
13+
public class CacheTest extends TestCase {
14+
15+
@Test
16+
public void testCacher() throws CacheException {
17+
int maxCount=1000;
18+
19+
Cache<Integer, Integer> cahe = new Cache<Integer, Integer>(CacheType.L1L2, (maxCount/2)-1, (maxCount/2)-1);
20+
21+
for(int i=0;i<maxCount;i++) {
22+
cahe.put(new Integer(i), new Integer(-i));
23+
}
24+
25+
assertNull("×èñëî "+1+" äîëæíî áûòü â êýø", cahe.get(1));
26+
assertNotNull("×èñëî "+(maxCount-1)+" äîëæíî áûòü â êýø", cahe.get(maxCount-1));
27+
assertNull("×èñëî "+(maxCount)+" íå äîëæíî áûòü â êýø", cahe.get(maxCount));
28+
29+
}
30+
31+
}

src/org/j262/cache/Cache.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.j262.cache;
2+
3+
import org.j262.cache.exception.CacheException;
4+
import org.j262.cache.map.CacheMap;
5+
import org.j262.cache.map.CacheMapDiskLRU;
6+
import org.j262.cache.map.CacheMapL1L2;
7+
import org.j262.cache.map.CacheMapMemLRU;
8+
9+
/**
10+
* @author batsura-sa
11+
*/
12+
13+
public class Cache<T,T1> {
14+
public enum CacheType { LRU_MEMORY, LRU_DISK, L1L2 };
15+
private CacheMap<T, T1> cache;
16+
private CacheType cacheType;
17+
18+
public Cache(CacheType cacheType, int maxSize) {
19+
this(cacheType, maxSize, maxSize);
20+
}
21+
22+
public Cache(CacheType cacheType, int maxSizeL1, int maxSizeL2) {
23+
24+
this.cacheType = cacheType;
25+
26+
switch (cacheType) {
27+
case LRU_MEMORY:
28+
this.cache = new CacheMapMemLRU<T, T1>(maxSizeL1);
29+
break;
30+
case LRU_DISK:
31+
this.cache = new CacheMapDiskLRU<T, T1>(maxSizeL1);
32+
break;
33+
case L1L2:
34+
this.cache = new CacheMapL1L2<T, T1>(maxSizeL1, maxSizeL2);
35+
break;
36+
37+
}
38+
}
39+
40+
public void put(T key, T1 value) throws CacheException {
41+
cache.put(key, value);
42+
}
43+
44+
public T1 get(T key) {
45+
return cache.get(key);
46+
}
47+
48+
public CacheEntry<T, T1> getLastDeletedEntry() {
49+
return cache.getLastDeletedEntry();
50+
}
51+
52+
public CacheType getCacheType() {
53+
return cacheType;
54+
}
55+
56+
}

src/org/j262/cache/CacheEntry.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.j262.cache;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* @author batsura-sa
7+
*/
8+
9+
public class CacheEntry<T,T1> implements Serializable {
10+
11+
private T key;
12+
private T1 value;
13+
private CacheEntryStat stat;
14+
15+
public CacheEntry(T key, T1 value) {
16+
this.key = key;
17+
this.value = value;
18+
this.stat = new CacheEntryStat();
19+
}
20+
21+
public T getKey() {
22+
return key;
23+
}
24+
25+
public T1 getValue() {
26+
stat.inc();
27+
return value;
28+
}
29+
30+
public void setValue(T1 value) {
31+
this.value = value;
32+
}
33+
34+
35+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.j262.cache;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* @author batsura-sa
7+
*/
8+
9+
public class CacheEntryStat implements Serializable {
10+
private volatile int count;
11+
private volatile long time;
12+
13+
public CacheEntryStat() {
14+
this.count = 0;
15+
this.time = 0;
16+
}
17+
18+
public int getCount() {
19+
return count;
20+
}
21+
22+
public void inc() {
23+
time=System.currentTimeMillis();
24+
count++;
25+
}
26+
27+
public long getTime() {
28+
return time;
29+
}
30+
31+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.j262.cache.demo;
2+
3+
import org.j262.cache.Cache;
4+
import org.j262.cache.Cache.CacheType;
5+
import org.j262.cache.exception.CacheException;
6+
7+
/**
8+
* @author batsura-sa
9+
*/
10+
11+
public class CacheDemo {
12+
13+
public static int MAX_COUNT=1000;
14+
15+
public static void main(String[] arg) throws CacheException {
16+
Cache<Integer, Integer> cahe = new Cache<Integer, Integer>(CacheType.L1L2, (MAX_COUNT/2)-1, (MAX_COUNT/2)-1);
17+
18+
for(int i=0;i<MAX_COUNT;i++) {
19+
cahe.put(new Integer(i), new Integer(-i));
20+
}
21+
22+
for(int i=0;i<MAX_COUNT;i++) {
23+
System.out.println( "i=" +i+ " value=" +cahe.get(new Integer(i)) );
24+
}
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.j262.cache.exception;
2+
3+
/**
4+
* @author batsura-sa
5+
*/
6+
7+
public class CacheException extends Exception {
8+
9+
public CacheException() {
10+
super();
11+
}
12+
13+
public CacheException(String message, Throwable cause,
14+
boolean enableSuppression, boolean writableStackTrace) {
15+
super(message, cause, enableSuppression, writableStackTrace);
16+
}
17+
18+
public CacheException(String message, Throwable cause) {
19+
super(message, cause);
20+
}
21+
22+
public CacheException(String message) {
23+
super(message);
24+
}
25+
26+
public CacheException(Throwable cause) {
27+
super(cause);
28+
}
29+
30+
}

src/org/j262/cache/map/CacheMap.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.j262.cache.map;
2+
3+
import org.j262.cache.CacheEntry;
4+
import org.j262.cache.exception.CacheException;
5+
6+
/**
7+
* @author batsura-sa
8+
*/
9+
10+
public interface CacheMap<T, T1> {
11+
public void put(T key, T1 value) throws CacheException;
12+
public T1 get(T key);
13+
public CacheEntry<T, T1> getLastDeletedEntry();
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.j262.cache.map;
2+
3+
/**
4+
* @author batsura-sa
5+
*/
6+
7+
//TODO Áàöóðà 28.07.2016 ñäåëàòü äèñêîâûé LRU cache
8+
public class CacheMapDiskLRU<T, T1> extends CacheMapMemLRU<T, T1> implements CacheMap<T, T1> {
9+
10+
public CacheMapDiskLRU(int maxSize) {
11+
super(maxSize);
12+
}
13+
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.j262.cache.map;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
import org.j262.cache.Cache;
7+
import org.j262.cache.Cache.CacheType;
8+
import org.j262.cache.CacheEntry;
9+
import org.j262.cache.exception.CacheException;
10+
11+
/**
12+
* @author batsura-sa
13+
*/
14+
15+
public class CacheMapL1L2<T, T1> implements CacheMap<T, T1> {
16+
private Cache<T, T1> l1;
17+
private Cache<T, T1> l2;
18+
private Lock lock;
19+
private CacheEntry<T, T1> lastDeletedCacheEntry;
20+
21+
22+
public CacheMapL1L2(int maxSizeL1, int maxSizeL2) {
23+
l1 = new Cache<T, T1>(CacheType.LRU_MEMORY, maxSizeL1);
24+
l2 = new Cache<T, T1>(CacheType.LRU_DISK, maxSizeL2);
25+
lock = new ReentrantLock();
26+
}
27+
28+
@Override
29+
public void put(T key, T1 value) throws CacheException {
30+
lock.lock();
31+
l1.put(key, value);
32+
CacheEntry<T, T1> cacheEntry=l1.getLastDeletedEntry();
33+
if ( cacheEntry!=null ) {
34+
l2.put(cacheEntry.getKey(), cacheEntry.getValue());
35+
lastDeletedCacheEntry=l2.getLastDeletedEntry();
36+
}
37+
lock.unlock();
38+
}
39+
40+
@Override
41+
public T1 get(T key) {
42+
T1 value=l1.get(key);
43+
if ( value!=null )
44+
return value;
45+
return l2.get(key);
46+
}
47+
48+
@Override
49+
public CacheEntry<T, T1> getLastDeletedEntry() {
50+
return lastDeletedCacheEntry;
51+
}
52+
}

0 commit comments

Comments
 (0)