1 /*
2 * Copyright 2012-2022 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.mybatis.caches.memcached;
17
18 import java.util.concurrent.locks.ReadWriteLock;
19
20 import org.apache.ibatis.cache.Cache;
21
22 /**
23 * The Memcached-based Cache implementation.
24 *
25 * @author Simone Tripodi
26 */
27 public final class MemcachedCache implements Cache {
28
29 private static final MemcachedClientWrapper MEMCACHED_CLIENT = new MemcachedClientWrapper();
30
31 /**
32 * The {@link ReadWriteLock}.
33 */
34 private final ReadWriteLock readWriteLock = new DummyReadWriteLock();
35
36 /**
37 * The cache id.
38 */
39 private final String id;
40
41 /**
42 * Builds a new Memcached-based Cache.
43 *
44 * @param id
45 * the Mapper id.
46 */
47 public MemcachedCache(final String id) {
48 this.id = id;
49 }
50
51 /**
52 * {@inheritDoc}
53 */
54 @Override
55 public void clear() {
56 MEMCACHED_CLIENT.removeGroup(this.id);
57 }
58
59 /**
60 * {@inheritDoc}
61 */
62 @Override
63 public String getId() {
64 return this.id;
65 }
66
67 /**
68 * {@inheritDoc}
69 */
70 @Override
71 public Object getObject(Object key) {
72 return MEMCACHED_CLIENT.getObject(key);
73 }
74
75 /**
76 * {@inheritDoc}
77 */
78 @Override
79 public ReadWriteLock getReadWriteLock() {
80 return this.readWriteLock;
81 }
82
83 /**
84 * {@inheritDoc}
85 */
86 @Override
87 public int getSize() {
88 return Integer.MAX_VALUE;
89 }
90
91 /**
92 * {@inheritDoc}
93 */
94 @Override
95 public void putObject(Object key, Object value) {
96 MEMCACHED_CLIENT.putObject(key, value, this.id);
97 }
98
99 /**
100 * {@inheritDoc}
101 */
102 @Override
103 public Object removeObject(Object key) {
104 return MEMCACHED_CLIENT.removeObject(key);
105 }
106
107 }