-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nigel Warren
authored and
Nigel Warren
committed
Feb 25, 2014
1 parent
44dc5ee
commit 12f165a
Showing
6 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.zink.cache; | ||
|
||
import java.io.Serializable; | ||
|
||
import com.zink.fly.Fly; | ||
|
||
public class Cache { | ||
|
||
private static final long DEFAULT_LEASE = 24*60*60*1000; | ||
|
||
private Fly fly; | ||
|
||
Cache(Fly fly) { | ||
this.fly = fly; | ||
} | ||
|
||
public final boolean set(String key, Serializable value) { | ||
setWithTimeout(key,value,DEFAULT_LEASE); | ||
return true; | ||
} | ||
|
||
|
||
public final Serializable get(String key) { | ||
CacheEntry tmpl = new CacheEntry(key); | ||
CacheEntry res = fly.read(tmpl, 0); | ||
if ( res != null) return res.v; | ||
return null; | ||
} | ||
|
||
public final boolean del(String key) { | ||
CacheEntry tmpl = new CacheEntry(key); | ||
if ( fly.take(tmpl, 0) != null ) return true; | ||
return false; | ||
} | ||
|
||
public boolean setnx(String key, Serializable value) { | ||
if ( get(key) == null) { | ||
set(key,value); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public boolean expire(String key, long timeOut) { | ||
Serializable value = get(key); | ||
if ( value != null) { | ||
setWithTimeout(key, value, timeOut); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
private final void setWithTimeout(String key, Serializable value, long timeout) { | ||
// evict a match to guarantee one only | ||
del(key); | ||
CacheEntry entry = new CacheEntry(key,value); | ||
fly.write(entry, timeout); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.zink.cache; | ||
|
||
import java.io.Serializable; | ||
|
||
public class CacheEntry { | ||
|
||
public String k; | ||
public Serializable v; | ||
|
||
public CacheEntry(String key, Serializable value ) { | ||
this.k = key; | ||
this.v = value; | ||
} | ||
|
||
public CacheEntry(String key) { | ||
this(key, null); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.zink.cache; | ||
|
||
import com.zink.fly.kit.FlyFinder; | ||
|
||
/** | ||
* The start point for gaining a connection to a Fly based message queue. | ||
* | ||
* Simply attempt to connect to a running Fly instance on the default localhost | ||
* or on the given host name or ip address | ||
*/ | ||
|
||
public class CacheFactory { | ||
|
||
public static Cache connect() { | ||
FlyFinder finder = new FlyFinder(); | ||
return new Cache(finder.find()); | ||
} | ||
|
||
public static Cache connect(String host) { | ||
FlyFinder finder = new FlyFinder(); | ||
return new Cache(finder.find(host)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.zink.cache; | ||
|
||
|
||
import java.math.BigInteger; | ||
import java.security.MessageDigest; | ||
|
||
// | ||
public class Hasher { | ||
|
||
public static BigInteger sha1(String str) { | ||
try { | ||
MessageDigest md = MessageDigest.getInstance("SHA1"); | ||
return new BigInteger(1,md.digest(str.getBytes("UTF-8"))); | ||
} | ||
catch(Exception exp) { | ||
throw new RuntimeException("Could not hash string"); | ||
} | ||
} | ||
|
||
public static BigInteger md5(String str) { | ||
try { | ||
MessageDigest md = MessageDigest.getInstance("md5"); | ||
return new BigInteger(1,md.digest(str.getBytes("UTF-8"))); | ||
} | ||
catch(Exception exp) { | ||
throw new RuntimeException("Could not hash string"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.zink.fly.example; | ||
|
||
|
||
import com.zink.cache.*; | ||
|
||
|
||
public class CacheShim { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
|
||
Cache cache = CacheFactory.connect(); | ||
|
||
cache.set("BBC1","http://www.bbc.co.uk/iplayer/tv/bbc_one"); | ||
|
||
System.out.println(cache.get("BBC1")); | ||
System.out.println(cache.get("BBC2")); | ||
|
||
cache.setnx("BBC1","junk"); | ||
System.out.println(cache.get("BBC1")); | ||
|
||
cache.del("BBC1"); | ||
System.out.println(cache.get("BBC1")); | ||
|
||
cache.setnx("BBC1","http://www.bbc.co.uk/iplayer/tv/bbc_one"); | ||
cache.expire("BBC1",100); | ||
System.out.println(cache.get("BBC1")); | ||
Thread.sleep(100); | ||
System.out.println(cache.get("BBC1")); | ||
|
||
System.out.println(Hasher.sha1("dennis")); | ||
System.out.println(Hasher.sha1("dennis")); | ||
System.out.println(Hasher.sha1("gnasher")); | ||
System.out.println(Hasher.md5("gnasher")); | ||
System.out.println(Hasher.md5("dennis")); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters