View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3    * use this file except in compliance with the License. You may obtain a copy of
4    * the License at
5    * 
6    * http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package org.esigate.cache;
16  
17  import java.io.IOException;
18  import java.net.InetSocketAddress;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  import java.util.Properties;
23  
24  import net.spy.memcached.MemcachedClient;
25  
26  import org.apache.http.impl.client.cache.CacheConfig;
27  import org.apache.http.impl.client.cache.memcached.MemcachedCacheEntryFactoryImpl;
28  import org.apache.http.impl.client.cache.memcached.MemcachedHttpCacheStorage;
29  import org.apache.http.impl.client.cache.memcached.SHA256KeyHashingScheme;
30  import org.esigate.ConfigurationException;
31  import org.esigate.Parameters;
32  
33  public class MemcachedCacheStorage extends CacheStorage {
34      @Override
35      public void init(Properties properties) {
36          Collection<String> serverStringList = Parameters.MEMCACHED_SERVERS_PROPERTY.getValue(properties);
37          if (serverStringList.isEmpty()) {
38              throw new ConfigurationException("No memcached server defined. Property '"
39                      + Parameters.MEMCACHED_SERVERS_PROPERTY + "' must be defined.");
40          }
41          List<InetSocketAddress> servers = new ArrayList<>();
42          for (String server : serverStringList) {
43              String[] serverHostPort = server.split(":");
44              if (serverHostPort.length != 2) {
45                  throw new ConfigurationException("Invalid memcached server: '" + server
46                          + "'. Each server must be in format 'host:port'.");
47              }
48              String host = serverHostPort[0];
49              try {
50                  int port = Integer.parseInt(serverHostPort[1]);
51                  servers.add(new InetSocketAddress(host, port));
52              } catch (NumberFormatException e) {
53                  throw new ConfigurationException("Invalid memcached server: '" + server
54                          + "'. Each server must be in format 'host:port'. Port must be an integer.", e);
55              }
56          }
57          MemcachedClient memcachedClient;
58          try {
59              memcachedClient = new MemcachedClient(servers);
60          } catch (IOException e) {
61              throw new ConfigurationException(e);
62          }
63          CacheConfig cacheConfig = CacheConfigHelper.createCacheConfig(properties);
64          setImpl(new MemcachedHttpCacheStorage(memcachedClient, cacheConfig, new MemcachedCacheEntryFactoryImpl(),
65                  new SHA256KeyHashingScheme()));
66      }
67  }