Clover icon

Magnolia Module Memcached 5.4.2

  1. Project Clover database Wed Aug 19 2015 15:04:32 CEST
  2. Package info.magnolia.cache.memcached.spy

File MemcachedFactory.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart5.png
50% of files have more coverage

Code metrics

2
12
4
1
136
78
7
0.58
3
4
1.75
45.5% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
MemcachedFactory 58 12 45.5% 7 10
0.4444444544.4%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.cache.memcached.spy;
35   
36    import info.magnolia.module.cache.Cache;
37    import info.magnolia.module.cache.CacheFactory;
38    import info.magnolia.objectfactory.ComponentProvider;
39   
40    import java.io.IOException;
41    import java.util.Arrays;
42    import java.util.HashMap;
43    import java.util.List;
44    import java.util.Map;
45   
46    import javax.inject.Inject;
47   
48    import org.slf4j.Logger;
49    import org.slf4j.LoggerFactory;
50   
51    import net.spy.memcached.AddrUtil;
52    import net.spy.memcached.MemcachedClient;
53    import net.spy.memcached.MemcachedClientIF;
54   
55    /**
56    * A CacheFactory based on {@link net.spy.memcached.MemcachedClient}.
57    */
 
58    public class MemcachedFactory implements CacheFactory {
59   
60    private static final Logger log = LoggerFactory.getLogger(MemcachedFactory.class);
61   
62    private ComponentProvider componentProvider;
63   
64    private int blockingTimeout = 10000;
65    private Class<? extends MemcachedWrapper> cacheWrapperImplementation = MemcachedWrapper.class;
66    private Map<String, MemcachedConnectionFactoryBuilder> caches = new HashMap<>();
67   
68    protected Map<String, MemcachedWrapper> clients = new HashMap<>();
69   
 
70  13 toggle @Inject
71    public MemcachedFactory(ComponentProvider componentProvider) {
72  13 this.componentProvider = componentProvider;
73    }
74   
75   
 
76  16 toggle @Override
77    public Cache getCache(String name) {
78  16 if (clients.containsKey(name)) {
79  4 return clients.get(name);
80    } else {
81  12 return clients.get(CacheFactory.DEFAULT_CACHE_NAME);
82    }
83    }
84   
 
85  0 toggle @Override
86    public void start(boolean isRestart) {
87  0 for (Map.Entry<String, MemcachedConnectionFactoryBuilder> cacheConfig : caches.entrySet()) {
88  0 try {
89  0 MemcachedClientIF client = new MemcachedClient(cacheConfig.getValue().build(), AddrUtil.getAddresses(cacheConfig.getValue().getServers()));
90  0 clients.put(cacheConfig.getKey(), componentProvider.newInstance(cacheWrapperImplementation, client, blockingTimeout, cacheConfig.getKey()));
91    } catch (IOException e) {
92  0 log.error("Cannot start memcached client for cache: {}", cacheConfig.getKey(), e);
93    }
94    }
95    }
96   
 
97    toggle @Override
98    public void stop(boolean isRestart) {
99    for (MemcachedWrapper cache : clients.values()) {
100    cache.shutdown();
101    }
102    }
103   
 
104    toggle @Override
105    public List<String> getCacheNames() {
106    return Arrays.asList(caches.keySet().toArray(new String[]{}));
107    }
108   
 
109    toggle public int getBlockingTimeout() {
110    return blockingTimeout;
111    }
112   
 
113    toggle public void setBlockingTimeout(int blockingTimeout) {
114    this.blockingTimeout = blockingTimeout;
115    }
116   
 
117    toggle public Map<String, MemcachedConnectionFactoryBuilder> getCaches() {
118    return caches;
119    }
120   
 
121    toggle public void setCaches(Map<String, MemcachedConnectionFactoryBuilder> caches) {
122    this.caches = caches;
123    }
124   
 
125    toggle public String getCacheWrapperImplementation() {
126    return cacheWrapperImplementation.getName();
127    }
128   
 
129  0 toggle public void setCacheWrapperImplementation(String cacheWrapperImplementation) {
130  0 try {
131  0 this.cacheWrapperImplementation = (Class<? extends MemcachedWrapper>) Class.forName(cacheWrapperImplementation);
132    } catch (ClassNotFoundException | ClassCastException e) {
133  0 log.error("Invalid implementation class for memcached wrapper: {}", cacheWrapperImplementation, e);
134    }
135    }
136    }