Skip to content

Commit

Permalink
Cache code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel Warren authored and Nigel Warren committed Feb 25, 2014
1 parent 44dc5ee commit 12f165a
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 2 deletions.
60 changes: 60 additions & 0 deletions src/com/zink/cache/Cache.java
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);
}
}
19 changes: 19 additions & 0 deletions src/com/zink/cache/CacheEntry.java
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);
}

}
24 changes: 24 additions & 0 deletions src/com/zink/cache/CacheFactory.java
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));
}

}
29 changes: 29 additions & 0 deletions src/com/zink/cache/Hasher.java
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");
}
}
}
38 changes: 38 additions & 0 deletions src/com/zink/fly/example/CacheShim.java
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"));

}

}
5 changes: 3 additions & 2 deletions src/com/zink/fly/example/MessageShim.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zink.fly.example;


import com.zink.queue.Connection;
import com.zink.queue.ConnectionFactory;
import com.zink.queue.ReadChannel;
Expand All @@ -8,13 +9,13 @@
public class MessageShim {

public static void main(String[] args) {

Connection con = ConnectionFactory.connect();

WriteChannel wc = con.publish("BBC7");
wc.write("Hello Subscriber");

ReadChannel rc = con.subscribe("BBC8");
ReadChannel rc = con.subscribe("BBC7");
System.out.println(rc.read());

}
Expand Down

0 comments on commit 12f165a

Please sign in to comment.