-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#226 Cache abstraction currently handles the 2 documented usecases
- Loading branch information
Thorsten Marx
committed
Aug 22, 2024
1 parent
191f2f4
commit 5d4d4e3
Showing
21 changed files
with
437 additions
and
34 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
cms-api/src/main/java/com/github/thmarx/cms/api/cache/CacheManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.github.thmarx.cms.api.cache; | ||
|
||
/*- | ||
* #%L | ||
* cms-api | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import java.io.Serializable; | ||
import java.time.Duration; | ||
import java.util.function.Function; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
@RequiredArgsConstructor | ||
public class CacheManager { | ||
private final CacheProvider cacheProvider; | ||
|
||
public <K extends Serializable, V extends Serializable> ICache<K, V> get (String name, CacheConfig config) { | ||
return cacheProvider.getCache(name, config); | ||
} | ||
|
||
public <K extends Serializable, V extends Serializable> ICache<K, V> get (String name, CacheConfig config, Function<K, V> loader) { | ||
return cacheProvider.getCache(name, config, loader); | ||
} | ||
|
||
public record CacheConfig (Long maxSize, Duration lifeTime) { | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
cms-api/src/main/java/com/github/thmarx/cms/api/cache/CacheProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github.thmarx.cms.api.cache; | ||
|
||
/*- | ||
* #%L | ||
* cms-api | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import java.io.Serializable; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
* @param <K> | ||
* @param <V> | ||
*/ | ||
public interface CacheProvider<K extends Serializable, V extends Serializable> { | ||
|
||
ICache<K, V> getCache (String name, CacheManager.CacheConfig config); | ||
|
||
ICache<K, V> getCache (String name, CacheManager.CacheConfig config, Function<K, V> loader); | ||
} |
44 changes: 44 additions & 0 deletions
44
cms-api/src/main/java/com/github/thmarx/cms/api/cache/ICache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.github.thmarx.cms.api.cache; | ||
|
||
/*- | ||
* #%L | ||
* cms-api | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
* @param <K> | ||
* @param <V> | ||
*/ | ||
public interface ICache<K extends Serializable, V extends Serializable> { | ||
|
||
void put (K key, V value); | ||
|
||
V get (K key); | ||
|
||
boolean contains (K key); | ||
|
||
void invalidate (); | ||
|
||
void invalidate (K key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
cms-core/src/main/java/com/github/thmarx/cms/core/cache/LocalCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.github.thmarx.cms.core.cache; | ||
|
||
/*- | ||
* #%L | ||
* cms-core | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import lombok.RequiredArgsConstructor; | ||
import com.github.thmarx.cms.api.cache.ICache; | ||
import java.io.Serializable; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
* @param <K> | ||
* @param <V> | ||
*/ | ||
@RequiredArgsConstructor | ||
public class LocalCache<K extends Serializable, V extends Serializable> implements ICache<K, V> { | ||
|
||
private final Cache<K,V> wrappedCache; | ||
private final Function<K, V> loader; | ||
|
||
@Override | ||
public void put(K key, V value) { | ||
wrappedCache.put(key, value); | ||
} | ||
|
||
@Override | ||
public V get(K key) { | ||
if (!contains(key)) { | ||
var value = loader.apply(key); | ||
if (value != null) { | ||
wrappedCache.put(key, value); | ||
} | ||
} | ||
return wrappedCache.getIfPresent(key); | ||
} | ||
|
||
@Override | ||
public boolean contains(K key) { | ||
return wrappedCache.getIfPresent(key) != null; | ||
} | ||
|
||
@Override | ||
public void invalidate() { | ||
wrappedCache.invalidateAll(); | ||
} | ||
|
||
@Override | ||
public void invalidate(K key) { | ||
wrappedCache.invalidate(key); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
cms-core/src/main/java/com/github/thmarx/cms/core/cache/LocalCacheProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.github.thmarx.cms.core.cache; | ||
|
||
/*- | ||
* #%L | ||
* cms-core | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.github.thmarx.cms.api.cache.CacheManager; | ||
import com.github.thmarx.cms.api.cache.CacheProvider; | ||
import com.github.thmarx.cms.api.cache.ICache; | ||
import java.io.Serializable; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public class LocalCacheProvider<K extends Serializable, V extends Serializable> implements CacheProvider<K, V> { | ||
|
||
private final ConcurrentMap<String, ICache<K, V>> caches = new ConcurrentHashMap<>(); | ||
|
||
private Cache<K,V> buildCache (CacheManager.CacheConfig config) { | ||
var cache = Caffeine.newBuilder(); | ||
|
||
if (config.maxSize() != null) { | ||
cache.maximumSize(config.maxSize()); | ||
} | ||
if (config.lifeTime() != null) { | ||
cache.expireAfterAccess(config.lifeTime()); | ||
cache.expireAfterWrite(config.lifeTime()); | ||
} | ||
|
||
return cache.build(); | ||
} | ||
|
||
@Override | ||
public ICache<K, V> getCache(String name, CacheManager.CacheConfig config) { | ||
if (!caches.containsKey(name)) { | ||
caches.putIfAbsent(name, new LocalCache( | ||
buildCache(config), | ||
(key) -> null)); | ||
} | ||
return caches.get(name); | ||
} | ||
|
||
@Override | ||
public ICache<K, V> getCache(String name, CacheManager.CacheConfig config, Function<K, V> loader) { | ||
if (!caches.containsKey(name)) { | ||
caches.putIfAbsent(name, new LocalCache( | ||
buildCache(config), | ||
loader)); | ||
} | ||
return caches.get(name); | ||
} | ||
|
||
} |
Oops, something went wrong.