├── .gitignore ├── LICENSE.txt ├── README.md ├── how-it-works.png ├── pom.xml └── src ├── main └── java │ └── org │ └── mvnsearch │ ├── Cache.java │ ├── Node.java │ └── SieveCache.java └── test └── java └── org └── mvnsearch └── SieveCacheTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.toptal.com/developers/gitignore/api/java,maven,jetbrains+all 2 | # Edit at https://www.toptal.com/developers/gitignore?templates=java,maven,jetbrains+all 3 | 4 | ### Java ### 5 | # Compiled class file 6 | *.class 7 | 8 | # Log file 9 | *.log 10 | 11 | # BlueJ files 12 | *.ctxt 13 | 14 | # Mobile Tools for Java (J2ME) 15 | .mtj.tmp/ 16 | 17 | # Package Files # 18 | *.jar 19 | *.war 20 | *.nar 21 | *.ear 22 | *.zip 23 | *.tar.gz 24 | *.rar 25 | 26 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 27 | hs_err_pid* 28 | replay_pid* 29 | 30 | ### JetBrains+all ### 31 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 32 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 33 | 34 | # User-specific stuff 35 | .idea/**/workspace.xml 36 | .idea/**/tasks.xml 37 | .idea/**/usage.statistics.xml 38 | .idea/**/dictionaries 39 | .idea/**/shelf 40 | 41 | # AWS User-specific 42 | .idea/**/aws.xml 43 | 44 | # Generated files 45 | .idea/**/contentModel.xml 46 | 47 | # Sensitive or high-churn files 48 | .idea/**/dataSources/ 49 | .idea/**/dataSources.ids 50 | .idea/**/dataSources.local.xml 51 | .idea/**/sqlDataSources.xml 52 | .idea/**/dynamic.xml 53 | .idea/**/uiDesigner.xml 54 | .idea/**/dbnavigator.xml 55 | 56 | # Gradle 57 | .idea/**/gradle.xml 58 | .idea/**/libraries 59 | 60 | # Gradle and Maven with auto-import 61 | # When using Gradle or Maven with auto-import, you should exclude module files, 62 | # since they will be recreated, and may cause churn. Uncomment if using 63 | # auto-import. 64 | # .idea/artifacts 65 | # .idea/compiler.xml 66 | # .idea/jarRepositories.xml 67 | # .idea/modules.xml 68 | # .idea/*.iml 69 | # .idea/modules 70 | *.iml 71 | *.ipr 72 | .idea/ 73 | 74 | # CMake 75 | cmake-build-*/ 76 | 77 | # Mongo Explorer plugin 78 | .idea/**/mongoSettings.xml 79 | 80 | # File-based project format 81 | *.iws 82 | 83 | # IntelliJ 84 | out/ 85 | 86 | # mpeltonen/sbt-idea plugin 87 | .idea_modules/ 88 | 89 | # JIRA plugin 90 | atlassian-ide-plugin.xml 91 | 92 | # Cursive Clojure plugin 93 | .idea/replstate.xml 94 | 95 | # SonarLint plugin 96 | .idea/sonarlint/ 97 | 98 | # Crashlytics plugin (for Android Studio and IntelliJ) 99 | com_crashlytics_export_strings.xml 100 | crashlytics.properties 101 | crashlytics-build.properties 102 | fabric.properties 103 | 104 | # Editor-based Rest Client 105 | .idea/httpRequests 106 | 107 | # Android studio 3.1+ serialized cache file 108 | .idea/caches/build_file_checksums.ser 109 | 110 | ### JetBrains+all Patch ### 111 | # Ignore everything but code style settings and run configurations 112 | # that are supposed to be shared within teams. 113 | 114 | .idea/* 115 | 116 | !.idea/codeStyles 117 | !.idea/runConfigurations 118 | 119 | ### Maven ### 120 | target/ 121 | pom.xml.tag 122 | pom.xml.releaseBackup 123 | pom.xml.versionsBackup 124 | pom.xml.next 125 | release.properties 126 | dependency-reduced-pom.xml 127 | buildNumber.properties 128 | .mvn/timing.properties 129 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar 130 | .mvn/wrapper/maven-wrapper.jar 131 | 132 | # Eclipse m2e generated files 133 | # Eclipse Core 134 | .project 135 | # JDT-specific (Eclipse Java Development Tools) 136 | .classpath 137 | 138 | # End of https://www.toptal.com/developers/gitignore/api/java,maven,jetbrains+all 139 | 140 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [2024] linux-china 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SIEVE Cache in Java 2 | =================== 3 | 4 | SIEVE is simpler than LRU with following features: 5 | 6 | * Simplicity: easy to implement and can be easily integrated into existing systems. 7 | * Efficiency: achieves state-of-the-art efficiency on skewed workloads. 8 | * Cache Primitive: facilitates the design of advanced eviction algorithms. 9 | 10 | ![How it works](how-it-works.png) 11 | 12 | # Get started 13 | 14 | * Add dependency to `pom.xml`: 15 | 16 | ```xml 17 | 18 | org.mvnsearch 19 | sieve-cache 20 | 0.1.0 21 | 22 | ``` 23 | * Create a cache instance and use it: 24 | 25 | ``` 26 | Cache cache = new SieveCache<>(); 27 | cache.put("nick", "Jackie"); 28 | System.out.println(cache.get("nick")); 29 | ``` 30 | 31 | # References 32 | 33 | * SIEVE: https://cachemon.github.io/SIEVE-website/ 34 | * SIEVE is simpler than LRU: https://cachemon.github.io/SIEVE-website/blog/2023/12/17/sieve-is-simpler-than-lru/ -------------------------------------------------------------------------------- /how-it-works.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linux-china/sieve-cache/4ac15c73ee9ce76085b929324118bba96e845642/how-it-works.png -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.mvnsearch 8 | sieve-cache 9 | 0.1.1-SNAPSHOT 10 | Sieve Cache Java 11 | Sieve: an Eviction Algorithm Simpler than LRU for Web Caches. 12 | https://github.com/linux-china/sieve-cache 13 | 14 | 15 | 1.8 16 | 1.8 17 | UTF-8 18 | 19 | 20 | 21 | 22 | linux_china 23 | Jacky Chan 24 | libing.chen@gmail.com 25 | https://twitter.com/linux_china 26 | 27 | Developer 28 | 29 | 30 | 31 | 32 | 33 | 34 | The Apache License, Version 2.0 35 | https://www.apache.org/licenses/LICENSE-2.0.txt 36 | 37 | 38 | 39 | 40 | scm:git:ssh://git@github.com:linux-china/sieve-cache.git 41 | scm:git:ssh://git@github.com:linux-china/sieve-cache.git 42 | https://github.com/linux-china/sieve-cache 43 | 44 | 45 | 46 | 47 | ossrh 48 | https://oss.sonatype.org/content/repositories/snapshots 49 | 50 | 51 | ossrh 52 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.jetbrains 60 | annotations 61 | 24.1.0 62 | 63 | 64 | org.junit.jupiter 65 | junit-jupiter 66 | 5.10.2 67 | test 68 | 69 | 70 | 71 | 72 | 73 | 74 | org.apache.maven.plugins 75 | maven-compiler-plugin 76 | 3.13.0 77 | 78 | true 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | release 88 | 89 | 90 | 91 | org.sonatype.plugins 92 | nexus-staging-maven-plugin 93 | 1.6.12 94 | true 95 | 96 | ossrh 97 | https://s01.oss.sonatype.org/ 98 | true 99 | 100 | 101 | 102 | org.apache.maven.plugins 103 | maven-source-plugin 104 | 3.2.1 105 | 106 | 107 | attach-sources 108 | 109 | jar-no-fork 110 | 111 | 112 | 113 | 114 | 115 | org.apache.maven.plugins 116 | maven-javadoc-plugin 117 | 3.1.1 118 | 119 | 120 | attach-javadocs 121 | 122 | jar 123 | 124 | 125 | 126 | 127 | 128 | org.apache.maven.plugins 129 | maven-gpg-plugin 130 | 1.6 131 | 132 | 133 | sign-artifacts 134 | verify 135 | 136 | sign 137 | 138 | 139 | 140 | --pinentry-mode 141 | loopback 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | -------------------------------------------------------------------------------- /src/main/java/org/mvnsearch/Cache.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch; 2 | 3 | import org.jetbrains.annotations.NotNull; 4 | import org.jetbrains.annotations.Nullable; 5 | 6 | import java.util.Map; 7 | import java.util.Set; 8 | 9 | /** 10 | * Cache API 11 | * 12 | * @author linux_china 13 | */ 14 | public interface Cache { 15 | 16 | void put(@NotNull String key, @NotNull T value); 17 | 18 | @Nullable 19 | T get(@NotNull String key); 20 | 21 | void delete(@NotNull String key); 22 | 23 | int capacity(); 24 | 25 | int size(); 26 | 27 | void clear(); 28 | 29 | boolean containsKey(@NotNull String key); 30 | 31 | Map getAll(Set keys); 32 | 33 | boolean putIfAbsent(@NotNull String key, @NotNull T value); 34 | 35 | @Nullable 36 | T getAndRemove(@NotNull String key); 37 | 38 | T getAndPut(@NotNull String key, @NotNull T value); 39 | 40 | boolean remove(String key, T oldValue); 41 | 42 | void removeAll(Set keys); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/mvnsearch/Node.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch; 2 | 3 | public class Node { 4 | private String key; 5 | private T value; 6 | private boolean visited = false; 7 | private Node prev = null; 8 | private Node next = null; 9 | 10 | public Node(String key, T value) { 11 | this.key = key; 12 | this.value = value; 13 | } 14 | 15 | public String getKey() { 16 | return key; 17 | } 18 | 19 | public void setKey(String key) { 20 | this.key = key; 21 | } 22 | 23 | public T getValue() { 24 | return value; 25 | } 26 | 27 | public void setValue(T value) { 28 | this.value = value; 29 | } 30 | 31 | public boolean isVisited() { 32 | return visited; 33 | } 34 | 35 | public void setVisited(boolean visited) { 36 | this.visited = visited; 37 | } 38 | 39 | public Node getPrev() { 40 | return prev; 41 | } 42 | 43 | public void setPrev(Node prev) { 44 | this.prev = prev; 45 | } 46 | 47 | public Node getNext() { 48 | return next; 49 | } 50 | 51 | public void setNext(Node next) { 52 | this.next = next; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/mvnsearch/SieveCache.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch; 2 | 3 | import org.jetbrains.annotations.NotNull; 4 | import org.jetbrains.annotations.Nullable; 5 | 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | import java.util.Set; 9 | import java.util.concurrent.ConcurrentHashMap; 10 | 11 | /** 12 | * Sieve Cache implementation 13 | * 14 | * @param object type 15 | * @author linux_china 16 | */ 17 | public class SieveCache implements Cache { 18 | private int capacity = 1000; 19 | private Node head = null; 20 | private Node tail = null; 21 | private Node hand = null; 22 | private int size = 0; 23 | private final Map> store = new ConcurrentHashMap<>(); 24 | 25 | public SieveCache() { 26 | 27 | } 28 | 29 | public SieveCache(int capacity) { 30 | this.capacity = capacity; 31 | } 32 | 33 | private void addToHead(@NotNull Node node) { 34 | node.setNext(this.head); 35 | node.setPrev(null); 36 | if (this.head != null) { 37 | this.head.setPrev(node); 38 | } 39 | this.head = node; 40 | if (this.tail == null) { 41 | this.tail = node; 42 | } 43 | } 44 | 45 | private void removeNode(@NotNull Node node) { 46 | if (node.getPrev() != null) { 47 | node.getPrev().setNext(node.getNext()); 48 | } else { 49 | this.head = node.getNext(); 50 | } 51 | if (node.getNext() != null) { 52 | node.getNext().setPrev(node.getPrev()); 53 | } else { 54 | this.tail = node.getPrev(); 55 | } 56 | } 57 | 58 | private void evict() { 59 | Node obj = this.hand != null ? this.hand : this.tail; 60 | while (obj != null && obj.isVisited()) { 61 | obj.setVisited(false); 62 | obj = obj.getPrev() != null ? obj.getPrev() : this.tail; 63 | } 64 | if (obj != null) { 65 | this.hand = obj.getPrev() != null ? obj.getPrev() : null; 66 | this.store.remove(obj.getKey()); 67 | this.removeNode(obj); 68 | this.size = this.size - 1; 69 | } 70 | } 71 | 72 | @Nullable 73 | public T get(@NotNull String key) { 74 | Node node = this.store.get(key); 75 | if (node != null) { 76 | node.setVisited(true); 77 | return node.getValue(); 78 | } 79 | return null; 80 | } 81 | 82 | public void put(@NotNull String key, @NotNull T value) { 83 | if (this.store.containsKey(key)) { 84 | this.store.get(key).setValue(value); 85 | } else { 86 | synchronized (this) { 87 | if (this.size >= this.capacity) { 88 | this.evict(); 89 | } 90 | Node node = new Node<>(key, value); 91 | this.addToHead(node); 92 | this.store.put(key, node); 93 | this.size = this.size + 1; 94 | node.setVisited(false); 95 | } 96 | } 97 | } 98 | 99 | public void delete(@NotNull String key) { 100 | if (this.store.containsKey(key)) { 101 | synchronized (this) { 102 | Node node = this.store.get(key); 103 | this.store.remove(key); 104 | this.removeNode(node); 105 | this.size = this.size - 1; 106 | } 107 | } 108 | } 109 | 110 | public void clear() { 111 | synchronized (this) { 112 | this.store.clear(); 113 | this.head = null; 114 | this.tail = null; 115 | this.hand = null; 116 | this.size = 0; 117 | } 118 | } 119 | 120 | public boolean containsKey(@NotNull String key) { 121 | return this.store.containsKey(key); 122 | } 123 | 124 | @Nullable 125 | public T getAndPut(@NotNull String key, @NotNull T value) { 126 | Node node = this.store.get(key); 127 | this.put(key, value); 128 | if (node != null) { 129 | return node.getValue(); 130 | } 131 | return null; 132 | } 133 | 134 | @Nullable 135 | public T getAndRemove(@NotNull String key) { 136 | Node node = this.store.get(key); 137 | if (node != null) { 138 | delete(key); 139 | return node.getValue(); 140 | } 141 | return null; 142 | } 143 | 144 | public boolean putIfAbsent(@NotNull String key, @NotNull T value) { 145 | if (!store.containsKey(key)) { 146 | put(key, value); 147 | return true; 148 | } 149 | return false; 150 | } 151 | 152 | public Map getAll(Set keys) { 153 | Map items = new HashMap<>(); 154 | for (String key : keys) { 155 | if (store.containsKey(key)) { 156 | items.put(key, store.get(key).getValue()); 157 | } 158 | } 159 | return items; 160 | } 161 | 162 | @Override 163 | public boolean remove(String key, T oldValue) { 164 | if (this.store.containsKey(key)) { 165 | final Node node = this.store.get(key); 166 | if (node.getValue().equals(oldValue)) { 167 | delete(key); 168 | return true; 169 | } 170 | } 171 | return false; 172 | } 173 | 174 | @Override 175 | public void removeAll(Set keys) { 176 | for (String key : keys) { 177 | delete(key); 178 | } 179 | } 180 | 181 | public void showItems() { 182 | Node current = this.head; 183 | while (current != null) { 184 | Node next = current.getNext(); 185 | System.out.println(current.getValue() + "(visited:" + current.isVisited() + ")" + (next != null ? (" -> " + next.getValue()) : "\n")); 186 | current = next; 187 | } 188 | } 189 | 190 | @Override 191 | public int capacity() { 192 | return this.capacity; 193 | } 194 | 195 | @Override 196 | public int size() { 197 | return this.store.size(); 198 | } 199 | } 200 | -------------------------------------------------------------------------------- /src/test/java/org/mvnsearch/SieveCacheTest.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.mvnsearch.SieveCache; 5 | 6 | public class SieveCacheTest { 7 | private static final SieveCache cache = new SieveCache<>(); 8 | 9 | @Test 10 | public void testCache() { 11 | cache.put("nick", "Jackie"); 12 | System.out.println(cache.get("nick")); 13 | cache.delete("nick"); 14 | System.out.println(cache.get("nick")); 15 | } 16 | 17 | @Test 18 | public void testShowCache() { 19 | cache.put("1", "First"); 20 | cache.put("2", "Two"); 21 | cache.put("3", "Three"); 22 | cache.put("4", "Four"); 23 | cache.showItems(); 24 | cache.delete("2"); 25 | System.out.println("========"); 26 | cache.showItems(); 27 | } 28 | 29 | @Test 30 | public void testCapacity() { 31 | SieveCache cache = new SieveCache<>(3); 32 | cache.put("1", "First"); 33 | cache.put("2", "Two"); 34 | cache.put("3", "Three"); 35 | cache.put("4", "Four"); 36 | cache.put("5", "Five"); 37 | cache.showItems(); 38 | } 39 | } 40 | --------------------------------------------------------------------------------