Last active
December 12, 2015 06:29
-
-
Save killme2008/4729789 to your computer and use it in GitHub Desktop.
A random session locator for xmemcached ,it can be used for kestrel.
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
package net.rubyeye.xmemcached.impl; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Random; | |
import com.google.code.yanf4j.core.Session; | |
import net.rubyeye.xmemcached.MemcachedSessionLocator; | |
/** | |
* A random session locator,it can be used in kestrel. | |
* | |
* @author dennis<killme2008@gmail.com> | |
* | |
*/ | |
public class RandomMemcachedSessionLocaltor implements MemcachedSessionLocator { | |
private transient volatile List<Session> sessions = Collections.emptyList(); | |
private final Random rand = new Random(); | |
public Session getSessionByKey(String key) { | |
List<Session> copiedOnWrite = sessions; | |
if (copiedOnWrite == null || copiedOnWrite.isEmpty()) | |
return null; | |
return copiedOnWrite.get(rand.nextInt(copiedOnWrite.size())); | |
} | |
public void updateSessions(Collection<Session> list) { | |
this.sessions = new ArrayList<Session>(list); | |
} | |
public void setFailureMode(boolean failureMode) { | |
// ignore | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment