-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from diegopacheco/dev
Cache Connections
- Loading branch information
Showing
15 changed files
with
156 additions
and
76 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
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
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
67 changes: 67 additions & 0 deletions
67
...main/java/com/github/diegopacheco/dynomite/cluster/checker/cluster/cache/ClientCache.java
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,67 @@ | ||
package com.github.diegopacheco.dynomite.cluster.checker.cluster.cache; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
import com.github.diegopacheco.dynomite.cluster.checker.cluster.DCCConnectionManager; | ||
import com.github.diegopacheco.dynomite.cluster.checker.parser.DynomiteNodeInfo; | ||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.RemovalListener; | ||
import com.google.common.cache.RemovalNotification; | ||
import com.netflix.dyno.jedis.DynoJedisClient; | ||
|
||
@Singleton | ||
public class ClientCache { | ||
|
||
private static Cache<String, DynoJedisClient> cache; | ||
private static final Logger logger = Logger.getLogger(ClientCache.class); | ||
|
||
static { | ||
cache = CacheBuilder.newBuilder(). | ||
maximumSize(500). | ||
expireAfterAccess(2, TimeUnit.HOURS). | ||
removalListener(new RemovalListener<String, DynoJedisClient>() { | ||
@Override | ||
public void onRemoval(RemovalNotification<String, DynoJedisClient> notification) { | ||
logger.debug("Removing... " + notification.getKey()); | ||
try { | ||
notification.getValue().stopClient(); | ||
} catch (Exception e) { | ||
logger.error("Error on close evicted client. EX: " + e); | ||
} | ||
} | ||
}).build(); | ||
} | ||
|
||
public static DynoJedisClient get(String seeds) { | ||
try { | ||
return cache.getIfPresent(seeds); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static void put(String seeds, DynoJedisClient client) { | ||
cache.put(seeds, client); | ||
} | ||
|
||
public static void main(String[] args) { | ||
String seeds = "127.0.0.1:8102:rack1:dc:100"; | ||
DynoJedisClient client = DCCConnectionManager.createSingleNodeCluster("test1",new DynomiteNodeInfo("127.0.0.1", "8102", "rack1", "dc", "100")); | ||
|
||
ClientCache.put(seeds, client); | ||
System.out.println(ClientCache.get(seeds)); | ||
System.out.println(ClientCache.get(seeds)); | ||
System.out.println(ClientCache.get(seeds)); | ||
|
||
String seeds2 = "127.0.0.1:8102:rack1:dc:0"; | ||
ClientCache.put(seeds2, client); | ||
System.out.println(ClientCache.get(seeds2)); | ||
System.out.println(ClientCache.get(seeds)); | ||
} | ||
|
||
} |
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
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
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
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
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
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
Oops, something went wrong.