View Javadoc
1   /*
2    *    Copyright 2016-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 issues.gh324;
17  
18  import org.apache.ibatis.cache.impl.PerpetualCache;
19  
20  public class ObservableCache extends PerpetualCache {
21  
22      private static ObservableCache instance;
23  
24      public static ObservableCache getInstance() {
25          return instance;
26      }
27  
28      private int requests = 0;
29  
30      public int getRequests() {
31          return requests;
32      }
33  
34      public int getHits() {
35          return hits;
36      }
37  
38      private int hits = 0;
39  
40      public ObservableCache(String id) {
41          super(id);
42          instance = this;
43      }
44  
45      @Override
46      public void putObject(Object key, Object value) {
47          super.putObject(key, value);
48      }
49  
50      @Override
51      public Object getObject(Object key) {
52          Object answer = super.getObject(key);
53  
54          if (key.toString().contains("select id, name from NameTable where id = ?")) {
55              requests++;
56  
57              if (answer != null) {
58                  hits++;
59              }
60          }
61  
62          return answer;
63      }
64  
65      @Override
66      public void clear() {
67          requests = 0;
68          hits = 0;
69          super.clear();
70      }
71  }