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

mybatis / ibatis-2 / 730

28 Dec 2025 10:16PM UTC coverage: 65.615% (+0.5%) from 65.146%
730

push

github

web-flow
Update README.md

1602 of 2802 branches covered (57.17%)

5053 of 7701 relevant lines covered (65.61%)

0.66 hits per line

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

65.96
/src/main/java/com/ibatis/sqlmap/engine/cache/memory/MemoryCacheController.java
1
/*
2
 * Copyright 2004-2025 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 com.ibatis.sqlmap.engine.cache.memory;
17

18
import com.ibatis.sqlmap.engine.cache.CacheController;
19
import com.ibatis.sqlmap.engine.cache.CacheModel;
20

21
import java.lang.ref.SoftReference;
22
import java.lang.ref.WeakReference;
23
import java.util.Collections;
24
import java.util.HashMap;
25
import java.util.Map;
26
import java.util.Properties;
27

28
/**
29
 * Memory-based implementation of CacheController.
30
 */
31
public class MemoryCacheController implements CacheController {
1✔
32

33
  /** The reference type. */
34
  private MemoryCacheLevel referenceType = MemoryCacheLevel.WEAK;
1✔
35

36
  /** The cache. */
37
  private Map cache = Collections.synchronizedMap(new HashMap<>());
1✔
38

39
  /**
40
   * Configures the cache
41
   *
42
   * @param props
43
   *          Optionally can contain properties [reference-type=WEAK|SOFT|STRONG]
44
   */
45
  @Override
46
  public void setProperties(Properties props) {
47
    String refType = props.getProperty("reference-type");
1✔
48
    if (refType == null) {
1!
49
      refType = props.getProperty("referenceType");
1✔
50
    }
51
    if (refType != null) {
1!
52
      referenceType = MemoryCacheLevel.getByReferenceType(refType);
×
53
    }
54
  }
1✔
55

56
  /**
57
   * Gets the reference type.
58
   *
59
   * @return the reference type
60
   */
61
  public MemoryCacheLevel getReferenceType() {
62
    return referenceType;
×
63
  }
64

65
  /**
66
   * Sets the reference type.
67
   *
68
   * @param referenceType
69
   *          the new reference type
70
   */
71
  public void setReferenceType(MemoryCacheLevel referenceType) {
72
    this.referenceType = referenceType;
×
73
  }
×
74

75
  /**
76
   * Add an object to the cache
77
   *
78
   * @param cacheModel
79
   *          The cacheModel
80
   * @param key
81
   *          The key of the object to be cached
82
   * @param value
83
   *          The object to be cached
84
   */
85
  @Override
86
  public void putObject(CacheModel cacheModel, Object key, Object value) {
87
    Object reference = null;
1✔
88
    if (referenceType.equals(MemoryCacheLevel.WEAK)) {
1!
89
      reference = new WeakReference(value);
1✔
90
    } else if (referenceType.equals(MemoryCacheLevel.SOFT)) {
×
91
      reference = new SoftReference(value);
×
92
    } else if (referenceType.equals(MemoryCacheLevel.STRONG)) {
×
93
      reference = new StrongReference(value);
×
94
    }
95
    cache.put(key, reference);
1✔
96
  }
1✔
97

98
  /**
99
   * Get an object out of the cache.
100
   *
101
   * @param cacheModel
102
   *          The cache model
103
   * @param key
104
   *          The key of the object to be returned
105
   *
106
   * @return The cached object (or null)
107
   */
108
  @Override
109
  public Object getObject(CacheModel cacheModel, Object key) {
110
    Object value = null;
1✔
111
    Object ref = cache.get(key);
1✔
112
    if (ref != null) {
1✔
113
      if (ref instanceof StrongReference) {
1!
114
        value = ((StrongReference) ref).get();
×
115
      } else if (ref instanceof SoftReference) {
1!
116
        value = ((SoftReference) ref).get();
×
117
      } else if (ref instanceof WeakReference) {
1!
118
        value = ((WeakReference) ref).get();
1✔
119
      }
120
    }
121
    return value;
1✔
122
  }
123

124
  @Override
125
  public Object removeObject(CacheModel cacheModel, Object key) {
126
    Object value = null;
1✔
127
    Object ref = cache.remove(key);
1✔
128
    if (ref != null) {
1!
129
      if (ref instanceof StrongReference) {
1!
130
        value = ((StrongReference) ref).get();
×
131
      } else if (ref instanceof SoftReference) {
1!
132
        value = ((SoftReference) ref).get();
×
133
      } else if (ref instanceof WeakReference) {
1!
134
        value = ((WeakReference) ref).get();
1✔
135
      }
136
    }
137
    return value;
1✔
138
  }
139

140
  /**
141
   * Flushes the cache.
142
   *
143
   * @param cacheModel
144
   *          The cache model
145
   */
146
  @Override
147
  public void flush(CacheModel cacheModel) {
148
    cache.clear();
1✔
149
  }
1✔
150

151
  /**
152
   * Class to implement a strong (permanent) reference.
153
   */
154
  private static class StrongReference {
155

156
    /** The object. */
157
    private Object object;
158

159
    /**
160
     * StrongReference constructor for an object.
161
     *
162
     * @param object
163
     *          - the Object to store
164
     */
165
    public StrongReference(Object object) {
×
166
      this.object = object;
×
167
    }
×
168

169
    /**
170
     * Getter to get the object stored in the StrongReference.
171
     *
172
     * @return - the stored Object
173
     */
174
    public Object get() {
175
      return object;
×
176
    }
177
  }
178

179
}
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