1+ /**
2+ * Copyright 2009-2019 the original author or authors.
3+ * <p>
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+ * <p>
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ * <p>
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 .apache .ibatis .cache ;
17+
18+ import org .apache .ibatis .cache .decorators .LruCache ;
19+ import org .apache .ibatis .cache .impl .PerpetualCache ;
20+ import org .junit .jupiter .api .Test ;
21+
22+ import static org .junit .jupiter .api .Assertions .*;
23+
24+ /**
25+ * LRU 缓存测试{@link LruCache}
26+ */
27+ class LruCacheTest {
28+
29+ @ Test
30+ void shouldRemoveLeastRecentlyUsedItemInBeyondFiveEntries () {
31+ LruCache cache = new LruCache (new PerpetualCache ("default" ));
32+ cache .setSize (5 );
33+ for (int i = 0 ; i < 5 ; i ++) {
34+ cache .putObject (i , i );
35+ }
36+ System .out .println (cache .getKeyMap ());
37+ assertEquals (0 , cache .getObject (0 ));
38+ cache .putObject (5 , 5 );
39+ System .out .println (cache .getKeyMap ());
40+ assertNull (cache .getObject (1 ));
41+ assertEquals (5 , cache .getSize ());
42+ }
43+
44+ @ Test
45+ void shouldRemoveItemOnDemand () {
46+ Cache cache = new LruCache (new PerpetualCache ("default" ));
47+ cache .putObject (0 , 0 );
48+ assertNotNull (cache .getObject (0 ));
49+ cache .removeObject (0 );
50+ assertNull (cache .getObject (0 ));
51+ }
52+
53+ @ Test
54+ void shouldFlushAllItemsOnDemand () {
55+ Cache cache = new LruCache (new PerpetualCache ("default" ));
56+ for (int i = 0 ; i < 5 ; i ++) {
57+ cache .putObject (i , i );
58+ }
59+ assertNotNull (cache .getObject (0 ));
60+ assertNotNull (cache .getObject (4 ));
61+ cache .clear ();
62+ assertNull (cache .getObject (0 ));
63+ assertNull (cache .getObject (4 ));
64+ }
65+
66+ }
0 commit comments