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