• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / ehcache-cache / #343

24 Sep 2023 01:01AM CUT coverage: 76.812%. Remained the same
#343

Pull #163

github

web-flow
Update dependency org.mybatis:mybatis-parent to v39
Pull Request #163: Update dependency org.mybatis:mybatis-parent to v39

53 of 69 relevant lines covered (76.81%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.68
/src/main/java/org/mybatis/caches/ehcache/AbstractEhcacheCache.java
1
/*
2
 *    Copyright 2010-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.ehcache;
17

18
import java.util.concurrent.locks.ReadWriteLock;
19

20
import net.sf.ehcache.CacheManager;
21
import net.sf.ehcache.Ehcache;
22
import net.sf.ehcache.Element;
23

24
import org.apache.ibatis.cache.Cache;
25

26
/**
27
 * Cache adapter for Ehcache.
28
 *
29
 * @author Simone Tripodi
30
 */
31
public abstract class AbstractEhcacheCache implements Cache {
32

33
  /**
34
   * The cache manager reference.
35
   */
36
  protected static CacheManager CACHE_MANAGER = CacheManager.create();
1✔
37

38
  /**
39
   * The cache id (namespace).
40
   */
41
  protected final String id;
42

43
  /**
44
   * The cache instance.
45
   */
46
  protected Ehcache cache;
47

48
  /**
49
   * Instantiates a new abstract ehcache cache.
50
   *
51
   * @param id
52
   *          the chache id (namespace)
53
   */
54
  public AbstractEhcacheCache(final String id) {
1✔
55
    if (id == null) {
1✔
56
      throw new IllegalArgumentException("Cache instances require an ID");
1✔
57
    }
58
    this.id = id;
1✔
59
  }
1✔
60

61
  /**
62
   * {@inheritDoc}
63
   */
64
  @Override
65
  public void clear() {
66
    cache.removeAll();
1✔
67
  }
1✔
68

69
  /**
70
   * {@inheritDoc}
71
   */
72
  @Override
73
  public String getId() {
74
    return id;
1✔
75
  }
76

77
  /**
78
   * {@inheritDoc}
79
   */
80
  @Override
81
  public Object getObject(Object key) {
82
    Element cachedElement = cache.get(key);
1✔
83
    if (cachedElement == null) {
1✔
84
      return null;
1✔
85
    }
86
    return cachedElement.getObjectValue();
1✔
87
  }
88

89
  /**
90
   * {@inheritDoc}
91
   */
92
  @Override
93
  public int getSize() {
94
    return cache.getSize();
1✔
95
  }
96

97
  /**
98
   * {@inheritDoc}
99
   */
100
  @Override
101
  public void putObject(Object key, Object value) {
102
    cache.put(new Element(key, value));
1✔
103
  }
1✔
104

105
  /**
106
   * {@inheritDoc}
107
   */
108
  @Override
109
  public Object removeObject(Object key) {
110
    Object obj = getObject(key);
1✔
111
    cache.remove(key);
1✔
112
    return obj;
1✔
113
  }
114

115
  /**
116
   * {@inheritDoc}
117
   */
118
  public void unlock(Object key) {
119
  }
×
120

121
  /**
122
   * {@inheritDoc}
123
   */
124
  @Override
125
  public boolean equals(Object obj) {
126
    if (this == obj) {
1✔
127
      return true;
1✔
128
    }
129
    if (obj == null) {
1✔
130
      return false;
×
131
    }
132
    if (!(obj instanceof Cache)) {
1✔
133
      return false;
1✔
134
    }
135

136
    Cache otherCache = (Cache) obj;
1✔
137
    return id.equals(otherCache.getId());
1✔
138
  }
139

140
  /**
141
   * {@inheritDoc}
142
   */
143
  @Override
144
  public int hashCode() {
145
    return id.hashCode();
1✔
146
  }
147

148
  @Override
149
  public ReadWriteLock getReadWriteLock() {
150
    return null;
×
151
  }
152

153
  /**
154
   * {@inheritDoc}
155
   */
156
  @Override
157
  public String toString() {
158
    return "EHCache {" + id + "}";
1✔
159
  }
160

161
  // DYNAMIC PROPERTIES
162

163
  /**
164
   * Sets the time to idle for an element before it expires. Is only used if the element is not eternal.
165
   *
166
   * @param timeToIdleSeconds
167
   *          the default amount of time to live for an element from its last accessed or modified date
168
   */
169
  public void setTimeToIdleSeconds(long timeToIdleSeconds) {
170
    cache.getCacheConfiguration().setTimeToIdleSeconds(timeToIdleSeconds);
1✔
171
  }
1✔
172

173
  /**
174
   * Sets the time to idle for an element before it expires. Is only used if the element is not eternal.
175
   *
176
   * @param timeToLiveSeconds
177
   *          the default amount of time to live for an element from its creation date
178
   */
179
  public void setTimeToLiveSeconds(long timeToLiveSeconds) {
180
    cache.getCacheConfiguration().setTimeToLiveSeconds(timeToLiveSeconds);
1✔
181
  }
1✔
182

183
  /**
184
   * Sets the maximum objects to be held in memory (0 = no limit).
185
   *
186
   * @param maxEntriesLocalHeap
187
   *          The maximum number of elements in heap, before they are evicted (0 == no limit)
188
   */
189
  public void setMaxEntriesLocalHeap(long maxEntriesLocalHeap) {
190
    cache.getCacheConfiguration().setMaxEntriesLocalHeap(maxEntriesLocalHeap);
1✔
191
  }
1✔
192

193
  /**
194
   * Sets the maximum number elements on Disk. 0 means unlimited.
195
   *
196
   * @param maxEntriesLocalDisk
197
   *          the maximum number of Elements to allow on the disk. 0 means unlimited.
198
   */
199
  public void setMaxEntriesLocalDisk(long maxEntriesLocalDisk) {
200
    cache.getCacheConfiguration().setMaxEntriesLocalDisk(maxEntriesLocalDisk);
1✔
201
  }
1✔
202

203
  /**
204
   * Sets the eviction policy. An invalid argument will set it to null.
205
   *
206
   * @param memoryStoreEvictionPolicy
207
   *          a String representation of the policy. One of "LRU", "LFU" or "FIFO".
208
   */
209
  public void setMemoryStoreEvictionPolicy(String memoryStoreEvictionPolicy) {
210
    cache.getCacheConfiguration().setMemoryStoreEvictionPolicy(memoryStoreEvictionPolicy);
1✔
211
  }
1✔
212

213
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc