|
| 1 | +package com.baeldung.infinispan.service; |
| 2 | + |
| 3 | +import com.baeldung.infinispan.listener.CacheListener; |
| 4 | +import com.baeldung.infinispan.repository.HelloWorldRepository; |
| 5 | +import org.infinispan.Cache; |
| 6 | + |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | + |
| 9 | +public class HelloWorldService { |
| 10 | + |
| 11 | + private final HelloWorldRepository repository; |
| 12 | + |
| 13 | + private final Cache<String, String> simpleHelloWorldCache; |
| 14 | + private final Cache<String, String> expiringHelloWorldCache; |
| 15 | + private final Cache<String, String> evictingHelloWorldCache; |
| 16 | + private final Cache<String, String> passivatingHelloWorldCache; |
| 17 | + |
| 18 | + public HelloWorldService(HelloWorldRepository repository, CacheListener listener, |
| 19 | + Cache<String, String> simpleHelloWorldCache, |
| 20 | + Cache<String, String> expiringHelloWorldCache, |
| 21 | + Cache<String, String> evictingHelloWorldCache, |
| 22 | + Cache<String, String> passivatingHelloWorldCache) { |
| 23 | + |
| 24 | + this.repository = repository; |
| 25 | + |
| 26 | + this.simpleHelloWorldCache = simpleHelloWorldCache; |
| 27 | + this.expiringHelloWorldCache = expiringHelloWorldCache; |
| 28 | + this.evictingHelloWorldCache = evictingHelloWorldCache; |
| 29 | + this.passivatingHelloWorldCache = passivatingHelloWorldCache; |
| 30 | + } |
| 31 | + |
| 32 | + public String findSimpleHelloWorld() { |
| 33 | + String cacheKey = "simple-hello"; |
| 34 | + String helloWorld = simpleHelloWorldCache.get(cacheKey); |
| 35 | + if (helloWorld == null) { |
| 36 | + helloWorld = repository.getHelloWorld(); |
| 37 | + simpleHelloWorldCache.put(cacheKey, helloWorld); |
| 38 | + } |
| 39 | + return helloWorld; |
| 40 | + } |
| 41 | + |
| 42 | + public String findExpiringHelloWorld() { |
| 43 | + String cacheKey = "expiring-hello"; |
| 44 | + String helloWorld = simpleHelloWorldCache.get(cacheKey); |
| 45 | + if (helloWorld == null) { |
| 46 | + helloWorld = repository.getHelloWorld(); |
| 47 | + simpleHelloWorldCache.put(cacheKey, helloWorld, 1, TimeUnit.SECONDS); |
| 48 | + } |
| 49 | + return helloWorld; |
| 50 | + } |
| 51 | + |
| 52 | + public String findIdleHelloWorld() { |
| 53 | + String cacheKey = "idle-hello"; |
| 54 | + String helloWorld = simpleHelloWorldCache.get(cacheKey); |
| 55 | + if (helloWorld == null) { |
| 56 | + helloWorld = repository.getHelloWorld(); |
| 57 | + simpleHelloWorldCache.put(cacheKey, helloWorld, -1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS); |
| 58 | + } |
| 59 | + return helloWorld; |
| 60 | + } |
| 61 | + |
| 62 | + public String findSimpleHelloWorldInExpiringCache() { |
| 63 | + String cacheKey = "simple-hello"; |
| 64 | + String helloWorld = expiringHelloWorldCache.get(cacheKey); |
| 65 | + if (helloWorld == null) { |
| 66 | + helloWorld = repository.getHelloWorld(); |
| 67 | + expiringHelloWorldCache.put(cacheKey, helloWorld); |
| 68 | + } |
| 69 | + return helloWorld; |
| 70 | + } |
| 71 | + |
| 72 | + public String findEvictingHelloWorld(String key) { |
| 73 | + String value = evictingHelloWorldCache.get(key); |
| 74 | + if(value == null) { |
| 75 | + value = repository.getHelloWorld(); |
| 76 | + evictingHelloWorldCache.put(key, value); |
| 77 | + } |
| 78 | + return value; |
| 79 | + } |
| 80 | + |
| 81 | + public String findPassivatingHelloWorld(String key) { |
| 82 | + String value = passivatingHelloWorldCache.get(key); |
| 83 | + if(value == null) { |
| 84 | + value = repository.getHelloWorld(); |
| 85 | + passivatingHelloWorldCache.put(key, value); |
| 86 | + } |
| 87 | + return value; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments