-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from rajumb0232/implementing/caching
Replaced Guava from EhCache for Caching
- Loading branch information
Showing
10 changed files
with
183 additions
and
77 deletions.
There are no files selected for viewing
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
32 changes: 32 additions & 0 deletions
32
E-Stores-API/src/main/java/com/devb/estores/cache/CacheBeanConfig.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,32 @@ | ||
package com.devb.estores.cache; | ||
|
||
import com.devb.estores.model.User; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.time.Duration; | ||
|
||
@Configuration | ||
public class CacheBeanConfig { | ||
|
||
private <K, V> CacheConfigurer<K, V> configure(String cacheName, Class<K> keyType, Class<V> valueType, int heap, int offHeap, Duration ttl) { | ||
return CacheConfigurer.<K, V>builder() | ||
.cacheName(cacheName) | ||
.keyType(keyType) | ||
.valueType(valueType) | ||
.heap(heap) | ||
.offHeap(offHeap) | ||
.ttl(ttl) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
CacheConfigurer<String, Integer> OtpCache() { | ||
return this.configure(CacheName.OTP_CACHE, String.class, Integer.class, 10000, 1, Duration.ofMinutes(5)); | ||
} | ||
|
||
@Bean | ||
CacheConfigurer<String, User> userCache() { | ||
return this.configure(CacheName.USER_CACHE, String.class, User.class, 20000, 10, Duration.ofDays(1)); | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
E-Stores-API/src/main/java/com/devb/estores/cache/CacheBeansConfig.java
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
E-Stores-API/src/main/java/com/devb/estores/cache/CacheConfigurer.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,43 @@ | ||
package com.devb.estores.cache; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.ehcache.config.CacheConfiguration; | ||
import org.ehcache.config.builders.CacheConfigurationBuilder; | ||
import org.ehcache.config.builders.ExpiryPolicyBuilder; | ||
import org.ehcache.config.builders.ResourcePoolsBuilder; | ||
import org.ehcache.config.units.EntryUnit; | ||
import org.ehcache.config.units.MemoryUnit; | ||
import org.ehcache.jsr107.Eh107Configuration; | ||
|
||
import javax.cache.configuration.Configuration; | ||
import java.time.Duration; | ||
|
||
@Getter | ||
@Builder | ||
@Slf4j | ||
public class CacheConfigurer<K, V> { | ||
private String cacheName; | ||
private Class<K> keyType; | ||
private Class<V> valueType; | ||
private int heap; | ||
private int offHeap; | ||
private Duration ttl; | ||
|
||
public Configuration<K, V> getEhCacheConfiguration() { | ||
log.info("Configuring Cache with name: {}, heap: {}, offHeap: {}, TTL: {}", this.cacheName, this.heap, this.offHeap, this.ttl.toMinutes() + " Minutes"); | ||
ResourcePoolsBuilder resourcePoolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder() | ||
.heap(this.heap, EntryUnit.ENTRIES).offheap(this.offHeap > 0 ? this.offHeap : 10, MemoryUnit.MB); | ||
|
||
CacheConfiguration<K, V> cacheConfiguration = CacheConfigurationBuilder | ||
.newCacheConfigurationBuilder( | ||
this.keyType, | ||
this.valueType, | ||
resourcePoolsBuilder) | ||
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(this.ttl)) | ||
.build(); | ||
|
||
return Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
E-Stores-API/src/main/java/com/devb/estores/cache/CacheInitializer.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,31 @@ | ||
package com.devb.estores.cache; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.cache.jcache.JCacheCacheManager; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.cache.CacheManager; | ||
import javax.cache.Caching; | ||
import javax.cache.spi.CachingProvider; | ||
import java.util.List; | ||
|
||
@Configuration | ||
@EnableCaching | ||
@Slf4j | ||
public class CacheInitializer { | ||
|
||
@Bean | ||
JCacheCacheManager cacheManager(List<CacheConfigurer<?,?>> cacheConfigurers) { | ||
log.info("Configuring CacheManger, {} cache configurations found", cacheConfigurers.size()); | ||
CachingProvider provider = Caching.getCachingProvider(); | ||
CacheManager manager = provider.getCacheManager(); | ||
|
||
cacheConfigurers.forEach(cacheConfigurer -> { | ||
manager.createCache(cacheConfigurer.getCacheName(), cacheConfigurer.getEhCacheConfiguration()); | ||
}); | ||
|
||
return new JCacheCacheManager(manager); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
E-Stores-API/src/main/java/com/devb/estores/cache/CacheName.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,6 @@ | ||
package com.devb.estores.cache; | ||
|
||
public class CacheName { | ||
public static final String OTP_CACHE = "otp-cache"; | ||
public static final String USER_CACHE = "user-cache"; | ||
} |
45 changes: 45 additions & 0 deletions
45
E-Stores-API/src/main/java/com/devb/estores/cache/CacheService.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,45 @@ | ||
package com.devb.estores.cache; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.springframework.cache.Cache; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
public class CacheService { | ||
|
||
private final CacheManager cacheManager; | ||
|
||
public <K, V> boolean doEntry(String cacheName, K key, V value) { | ||
Cache cache = cacheManager.getCache(cacheName); | ||
if(cache != null) { | ||
cache.put(key, value); | ||
return true; | ||
} else return false; | ||
} | ||
|
||
public <K, V> V getEntry(String cacheName, K key, Class<V> type) { | ||
Cache cache = cacheManager.getCache(cacheName); | ||
if(cache != null) | ||
return cache.get(key, type); | ||
else return null; | ||
} | ||
|
||
public <K> boolean evictEntry(String cacheName, K key) { | ||
Cache cache = cacheManager.getCache(cacheName); | ||
if(cache != null) { | ||
cache.evict(key); | ||
return true; | ||
} else return false; | ||
} | ||
|
||
public boolean clearCache(String cacheName) { | ||
Cache cache = cacheManager.getCache(cacheName); | ||
if (cache != null) { | ||
cache.clear(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
E-Stores-API/src/main/java/com/devb/estores/cache/CacheStore.java
This file was deleted.
Oops, something went wrong.
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