Skip to content

Commit 57cd7b3

Browse files
author
Rai
committed
files pushed to repo
1 parent 4be8265 commit 57cd7b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1417
-0
lines changed

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Intellij
2+
.idea/
3+
*.iml
4+
*.iws
5+
*.ipr
6+
7+
# Mac
8+
.DS_Store
9+
10+
# Package Files #
11+
*.jar
12+
*.war
13+
*.ear
14+
15+
# Gradle
16+
.gradle
17+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored).
18+
# Add this 'after' *.jar ignore
19+
!gradle-wrapper.jar
20+
21+
# Binaries
22+
build
23+
target
24+
out
25+
classes
26+
reports
27+
bin
28+
*.class
29+
30+
#Eclipse
31+
.classpath
32+
.project
33+
.settings
34+
35+
# Vagrant, Ansible and Docker
36+
.vagrant
37+
.docker
38+
*.retry

jcs_swap/JsonDocCache.data

Whitespace-only changes.

jcs_swap/JsonDocCache.key

Whitespace-only changes.

jcs_swap/default.data

1.68 KB
Binary file not shown.

jcs_swap/default.key

271 Bytes
Binary file not shown.

pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>com.delta</groupId>
8+
<artifactId>core</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>1.8</source>
17+
<target>1.8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
<dependencies>
24+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-jcs-core -->
25+
<dependency>
26+
<groupId>org.apache.commons</groupId>
27+
<artifactId>commons-jcs-core</artifactId>
28+
<version>2.0-beta-2</version>
29+
</dependency>
30+
31+
32+
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
33+
<dependency>
34+
<groupId>commons-logging</groupId>
35+
<artifactId>commons-logging</artifactId>
36+
<version>1.2</version>
37+
</dependency>
38+
39+
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
40+
<dependency>
41+
<groupId>org.projectlombok</groupId>
42+
<artifactId>lombok</artifactId>
43+
<version>1.16.8</version>
44+
</dependency>
45+
46+
47+
<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
48+
<dependency>
49+
<groupId>net.sf.ehcache</groupId>
50+
<artifactId>ehcache</artifactId>
51+
<version>2.10.3</version>
52+
</dependency>
53+
54+
<!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
55+
<dependency>
56+
<groupId>org.ehcache</groupId>
57+
<artifactId>ehcache</artifactId>
58+
<version>3.2.0</version>
59+
</dependency>
60+
61+
62+
<!--loggin-->
63+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
64+
<dependency>
65+
<groupId>org.slf4j</groupId>
66+
<artifactId>slf4j-api</artifactId>
67+
<version>1.7.22</version>
68+
</dependency>
69+
70+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
71+
<dependency>
72+
<groupId>org.slf4j</groupId>
73+
<artifactId>slf4j-simple</artifactId>
74+
<version>1.7.22</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.springframework</groupId>
78+
<artifactId>spring-context</artifactId>
79+
<version>4.3.4.RELEASE</version>
80+
</dependency>
81+
82+
83+
</dependencies>
84+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.eprogrammerz.examples.cache.example.cloning;
2+
3+
import java.util.Date;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* Created by 542596 on 12/21/2016.
9+
*/
10+
public class HashMapClone {
11+
public static void main(String[] args) {
12+
HashMap<String, Object> map = new HashMap<>();
13+
map.put("key1", "value1");
14+
map.put("key2", "value2");
15+
map.put("key3", "value3");
16+
17+
HashMap<String, Object> clonedMap = (HashMap<String, Object>) map.clone();
18+
19+
System.out.println(map);
20+
System.out.println(clonedMap);
21+
22+
System.out.println("Changing value...");
23+
map.remove("key1");
24+
map = clonedMap;
25+
System.out.println(map);
26+
System.out.println(clonedMap);
27+
28+
Date date = new Date(1482421944086L);
29+
System.out.println(date);
30+
}
31+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.eprogrammerz.examples.cache.example.ehcache;
2+
3+
/**
4+
* Created by 542596 on 12/12/2016.
5+
*/
6+
7+
8+
public class Application {
9+
10+
public static void main(String[] args) {
11+
JsonObjCacheManager manager = new JsonObjCacheManager();
12+
13+
// manager.putInCache("key1", "Delta Airlines");
14+
// String storedValue = (String) manager.retrieveFromCache("key1");
15+
// System.out.println(storedValue);
16+
17+
final City city1 = new City("ATL","USA",12100);
18+
final City city2 = new City("FL","USA",12000);
19+
20+
/* manager.putInCache(city1.getName(), new HashMap(){
21+
{
22+
put("city1", city1);
23+
}
24+
});
25+
manager.putInCache(city2.getName(), new HashMap(){
26+
{
27+
put("city2", city2);
28+
}
29+
});*/
30+
31+
manager.putInCache(city1.getName(), city1);
32+
manager.putInCache(city2.getName(), city2);
33+
34+
System.out.println(manager.getKeys());
35+
36+
for(String key: manager.getKeys()){
37+
System.out.println(key + ": "+ manager.retrieveFromCache(key));
38+
}
39+
40+
// HashMap fromCache1 = (HashMap) manager.retrieveFromCache(city1.getName());
41+
// HashMap fromCache = (HashMap) fromCache1.clone();
42+
City cityFromCache = (City) manager.retrieveFromCache(city1.getName());
43+
cityFromCache.setName("KTM");
44+
cityFromCache.setCountry("NPL");
45+
System.out.println(manager.getKeys());
46+
47+
for(String key: manager.getKeys()){
48+
System.out.println(key + ": "+ manager.retrieveFromCache(key));
49+
}
50+
}
51+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.eprogrammerz.examples.cache.example.ehcache;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import lombok.ToString;
7+
8+
import java.io.Serializable;
9+
10+
/**
11+
* Created by 542596 on 12/9/2016.
12+
*/
13+
@Getter
14+
@Setter
15+
@ToString
16+
@AllArgsConstructor
17+
public class City implements Serializable {
18+
public String name;
19+
public String country;
20+
public int population;
21+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.eprogrammerz.examples.cache.example.ehcache;
2+
3+
import net.sf.ehcache.Cache;
4+
import net.sf.ehcache.CacheException;
5+
import net.sf.ehcache.CacheManager;
6+
import net.sf.ehcache.Element;
7+
import net.sf.ehcache.config.CacheConfiguration;
8+
import net.sf.ehcache.config.PersistenceConfiguration;
9+
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import java.util.List;
14+
15+
/**
16+
* Created by 542596 on 12/12/2016.
17+
*/
18+
public class JsonObjCacheManager {
19+
private static final Logger logger = LoggerFactory.getLogger(JsonObjCacheManager.class);
20+
private CacheManager manager;
21+
22+
private Cache objectCache;
23+
24+
public JsonObjCacheManager(){
25+
manager = CacheManager.create();
26+
27+
objectCache = manager.getCache("jsonDocCache");
28+
29+
if( objectCache == null){
30+
objectCache = new Cache(
31+
new CacheConfiguration("jsonDocCache", 1000)
32+
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU)
33+
.eternal(false)
34+
.timeToLiveSeconds(60 * 60)
35+
.timeToIdleSeconds(0)
36+
.diskExpiryThreadIntervalSeconds(0)
37+
.persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP)));
38+
objectCache.disableDynamicFeatures();
39+
manager.addCache(objectCache);
40+
}
41+
}
42+
43+
public List<String> getKeys() { return objectCache.getKeys();}
44+
45+
public void clearCache(){
46+
manager.removeAllCaches();
47+
}
48+
49+
public void putInCache(String key, Object value){
50+
try{
51+
objectCache.put(new Element(key, value));
52+
}catch (CacheException e){
53+
logger.error(String.format( "Problem occurred while putting data into cache: %s", e.getMessage()));
54+
}
55+
}
56+
57+
public Object retrieveFromCache(String key){
58+
// System.out.println("+++++++++++");
59+
// System.out.println(objectCache.getKeys());
60+
try {
61+
Element element = objectCache.get(key);
62+
if(element != null)
63+
return element.getObjectValue();
64+
}catch (CacheException ce){
65+
logger.error(String.format("Problem occurred while trying to retrieveSpecific from cache: %s", ce.getMessage()));
66+
}
67+
return null;
68+
}
69+
}

0 commit comments

Comments
 (0)