From 3f106523619ac1ff9d5509e95707da1a9626038f Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 23 Jun 2016 14:18:07 +1000 Subject: [PATCH] Allow a list of memcached servers to be configured for session caching. --- .../session-data-cache/xmemcached.xml | 21 ++++++-- .../session/MemcachedSessionDataMap.java | 12 +++++ .../MemcachedSessionDataMapFactory.java | 52 +++++++++---------- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/jetty-memcached/jetty-memcached-sessions/src/main/config/etc/sessions/session-data-cache/xmemcached.xml b/jetty-memcached/jetty-memcached-sessions/src/main/config/etc/sessions/session-data-cache/xmemcached.xml index 2b957f62760..a800887602e 100644 --- a/jetty-memcached/jetty-memcached-sessions/src/main/config/etc/sessions/session-data-cache/xmemcached.xml +++ b/jetty-memcached/jetty-memcached-sessions/src/main/config/etc/sessions/session-data-cache/xmemcached.xml @@ -3,8 +3,23 @@ - - - + + + + + + + + + + + + + + + 100 + + + diff --git a/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMap.java b/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMap.java index 16869a53e09..ef206e6c0a4 100644 --- a/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMap.java +++ b/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMap.java @@ -19,6 +19,8 @@ package org.eclipse.jetty.memcached.session; import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.List; import org.eclipse.jetty.server.session.SessionContext; import org.eclipse.jetty.server.session.SessionData; @@ -58,6 +60,16 @@ public class MemcachedSessionDataMap extends AbstractLifeCycle implements Sessio } + public MemcachedSessionDataMap (List addresses) + { + _builder = new XMemcachedClientBuilder(addresses); + } + + + public MemcachedSessionDataMap (List addresses, int[] weights) + { + _builder = new XMemcachedClientBuilder(addresses, weights); + } /** * @return the builder diff --git a/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMapFactory.java b/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMapFactory.java index e67bef94516..da91aac9d97 100644 --- a/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMapFactory.java +++ b/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMapFactory.java @@ -18,6 +18,10 @@ package org.eclipse.jetty.memcached.session; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; + import org.eclipse.jetty.server.session.SessionDataMap; import org.eclipse.jetty.server.session.SessionDataMapFactory; @@ -28,44 +32,40 @@ import org.eclipse.jetty.server.session.SessionDataMapFactory; */ public class MemcachedSessionDataMapFactory implements SessionDataMapFactory { - protected String _host = "localhost"; - protected String _port = "11211"; protected int _expiry; + protected int[] _weights; + protected List _addresses; + /** + * @param addresses host and port address of memcached servers + */ + public void setAddresses(InetSocketAddress... addresses) + { + if (addresses == null) + _addresses = null; + _addresses = new ArrayList<>(); + for (InetSocketAddress a:addresses) + _addresses.add(a); + } - - - public String getHost() + /** + * @param weights the relative weight to give each server in the list of addresses + */ + public void setWeights(int[] weights) { - return _host; + _weights = weights; } - public void setHost(String host) - { - _host = host; - } - - - public String getPort() - { - return _port; - } - - - public void setPort(String port) - { - _port = port; - } - - - public int getExpirySec() { return _expiry; } + /** + * @param expiry time in secs that memcached item remains valid + */ public void setExpirySec(int expiry) { _expiry = expiry; @@ -77,7 +77,7 @@ public class MemcachedSessionDataMapFactory implements SessionDataMapFactory @Override public SessionDataMap getSessionDataMap() { - MemcachedSessionDataMap m = new MemcachedSessionDataMap(_host, _port); + MemcachedSessionDataMap m = new MemcachedSessionDataMap(_addresses, _weights); m.setExpirySec(_expiry); return m; }