Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ework

� Conflicts:
�	crud-framework-core/src/main/java/com/antelopesystem/crudframework/crud/annotation/CachedBy.kt
  • Loading branch information
Idane committed Jun 19, 2021
2 parents be0c67e + 8ea14c4 commit f913f34
Show file tree
Hide file tree
Showing 49 changed files with 847 additions and 123 deletions.
63 changes: 63 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
version: 2
updates:
- package-ecosystem: maven
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
ignore:
- dependency-name: mysql:mysql-connector-java
versions:
- "> 5.1.18"
- dependency-name: org.apache.commons:commons-lang3
versions:
- ">= 3.11.a, < 3.12"
- dependency-name: org.aspectj:aspectjweaver
versions:
- "> 1.8.13"
- dependency-name: org.hibernate:hibernate-core
versions:
- "> 5.2.17.Final"
- dependency-name: org.hibernate.validator:hibernate-validator
versions:
- "> 6.1.0.Final"
- dependency-name: org.jetbrains.dokka:dokka-maven-plugin
versions:
- "> 1.4.20, < 1.5"
- dependency-name: org.jetbrains.kotlin:kotlin-maven-allopen
versions:
- "> 1.3.20"
- dependency-name: org.jetbrains.kotlin:kotlin-maven-noarg
versions:
- "> 1.3.20"
- dependency-name: org.jetbrains.kotlin:kotlin-maven-plugin
versions:
- "> 1.3.20"
- dependency-name: org.jetbrains.kotlin:kotlin-reflect
versions:
- "> 1.3.20"
- dependency-name: org.jetbrains.kotlin:kotlin-stdlib-jdk8
versions:
- "> 1.3.20"
- dependency-name: org.springframework.boot:spring-boot-starter-parent
versions:
- "> 2.0.8.RELEASE"
- dependency-name: org.testcontainers:mysql
versions:
- "> 1.10.6"
- dependency-name: org.antlr:antlr4-runtime
versions:
- 4.9.2
- dependency-name: org.jetbrains.kotlinx:kotlinx-coroutines-core
versions:
- 1.4.2-native-mt
- 1.4.3-native-mt
- dependency-name: org.jetbrains.dokka:dokka-maven-plugin
versions:
- 1.4.20-dev-65
- dependency-name: org.apache.commons:commons-lang3
versions:
- "3.10"
- dependency-name: net.sf.ehcache:ehcache
versions:
- 2.10.6
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ Maven:
<dependency>
<groupId>com.antelopesystem.crudframework</groupId>
<artifactId>crud-framework-hibernate5-connector</artifactId>
<version>0.3.1</version>
<version>0.4.1</version>
</dependency>
```

Gradle:

```kotlin
implementation("com.antelopesystem.crudframework:crud-framework-hibernate5-connector:0.3.1")
implementation("com.antelopesystem.crudframework:crud-framework-hibernate5-connector:0.4.1")
```

### MongoDB Connector
Expand All @@ -44,14 +44,14 @@ implementation("com.antelopesystem.crudframework:crud-framework-hibernate5-conne
<dependency>
<groupId>com.antelopesystem.crudframework</groupId>
<artifactId>crud-framework-mongo-connector</artifactId>
<version>0.3.1</version>
<version>0.4.1</version>
</dependency>
```

Gradle:

```kotlin
implementation("com.antelopesystem.crudframework:crud-framework-mongo-connector:0.3.1")
implementation("com.antelopesystem.crudframework:crud-framework-mongo-connector:0.4.1")
```


Expand Down
7 changes: 4 additions & 3 deletions crud-framework-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.antelopesystem.crudframework</groupId>
<artifactId>crud-framework</artifactId>
<version>0.3.5-SNAPSHOT</version>
<version>0.4.4-SNAPSHOT</version>
</parent>
<dependencies>

Expand All @@ -23,6 +23,7 @@
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache.version}</version>
<optional>true</optional>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
Expand Down Expand Up @@ -52,13 +53,13 @@
<dependency>
<groupId>com.antelopesystem.crudframework</groupId>
<artifactId>crud-framework-utils</artifactId>
<version>0.3.5-SNAPSHOT</version>
<version>0.4.4-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.antelopesystem.crudframework</groupId>
<artifactId>crud-framework-fieldmapper</artifactId>
<version>0.3.5-SNAPSHOT</version>
<version>0.4.4-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,29 @@ package com.antelopesystem.crudframework.crud.annotation
@Target(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS)
annotation class CachedBy(
/**
* Cache name in the underlying cache provider
* The name of the cache
*/
val value: String
val value: String,
/**
* Whether or not to create a cache with this name if the cache does not exist
*/
val createIfMissing: Boolean = false,

/**
* Specifies time to live in seconds when creating the cache if missing, may not be supported on all providers.
* -1 is interpret as null
*/
val timeToLiveSeconds: Long = -1L,

/**
* Specifies time to idle in seconds when creating the cache if missing, may not be supported on all providers
* -1 is interpret as null
*/
val timeToIdleSeconds: Long = -1L,

/**
* Specifies max cache entries when creating the cache if missing, may not be supported on all providers
* -1 is interpret as null
*/
val maxEntries: Long = -1
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.antelopesystem.crudframework.crud.annotation

import com.antelopesystem.crudframework.utils.component.startup.annotation.EnablePostStartup
import com.antelopesystem.crudframework.utils.component.componentmap.annotation.EnableComponentMap
import com.antelopesystem.crudframework.crud.configuration.CacheConfiguration
import com.antelopesystem.crudframework.crud.configuration.CrudFrameworkConfiguration
import com.antelopesystem.crudframework.utils.component.componentmap.annotation.EnableComponentMap
import com.antelopesystem.crudframework.utils.component.startup.annotation.EnablePostStartup
import org.springframework.context.annotation.Import


Expand All @@ -12,7 +13,7 @@ import org.springframework.context.annotation.Import
* Additionally triggers [CrudFrameworkConfiguration]
*/
@Target(AnnotationTarget.CLASS)
@Import(CrudFrameworkConfiguration::class)
@Import(CrudFrameworkConfiguration::class, CacheConfiguration::class)
@EnableComponentMap
@EnablePostStartup
annotation class EnableCrudFramework
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.antelopesystem.crudframework.crud.cache

/**
* Used to retrieve and create caches
*/
interface CacheManagerAdapter {
/**
* Get a cache by its name
*/
fun getCache(name: String): CrudCache?

/**
* Create a cache with the given name and options
* Not all options are supported by all vendors
* Whether or not the cache exists or not and how to handle that is passed on to the vendor
*/
fun createCache(name: String, options: CrudCacheOptions): CrudCache
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package com.antelopesystem.crudframework.utils.utils;
package com.antelopesystem.crudframework.crud.cache;


import org.springframework.cache.Cache;

import java.util.function.Supplier;

public class CacheUtils {

public static void removeFromCacheIfKeyContains(Cache cache, String subKey) {
cache.evict(subKey);
public static void removeFromCacheIfKeyContains(CrudCache cache, String subKey) {
cache.remove(subKey);
}

public static Object getObjectAndCache(Supplier<Object> objectSupplier, String key, Cache cache) {
public static Object getObjectAndCache(Supplier<Object> objectSupplier, String key, CrudCache cache) {
if(cache == null) {
return objectSupplier.get();
}

Cache.ValueWrapper cached = cache.get(key);
Object cached = cache.get(key);
Object result;
if(cached == null) {
result = objectSupplier.get();
cache.put(key, result);
} else {
result = cached.get();
result = cached;
}

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.antelopesystem.crudframework.crud.cache

/**
* Internal cache abstraction
*/
interface CrudCache {
fun get(key: Any): Any?
fun put(key: Any, value: Any?)
fun remove(key: Any)
fun removeAll()
fun unwrap() : Any
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.antelopesystem.crudframework.crud.cache

/**
* Options available when creating a cache
* Not all options are supported by all vendors
*/
data class CrudCacheOptions(
val timeToLiveSeconds: Long? = null,
val timeToIdleSeconds: Long? = null,
val maxEntries: Long? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.antelopesystem.crudframework.crud.cache.adapter.ehcache

import com.antelopesystem.crudframework.crud.cache.CrudCache
import net.sf.ehcache.Ehcache
import net.sf.ehcache.Element

class CrudEhCacheImpl(private val vendorCache: Ehcache) : CrudCache {
override fun get(key: Any): Any? {
return vendorCache.get(key)?.objectValue
}

override fun put(key: Any, value: Any?) {
val element = Element(key, value)
vendorCache.put(element)
}

override fun remove(key: Any) {
vendorCache.remove(key)
}

override fun removeAll() {
vendorCache.removeAll()
}

override fun unwrap(): Any {
return vendorCache
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.antelopesystem.crudframework.crud.cache.adapter.ehcache

import com.antelopesystem.crudframework.crud.cache.CacheManagerAdapter
import com.antelopesystem.crudframework.crud.cache.CrudCache
import com.antelopesystem.crudframework.crud.cache.CrudCacheOptions
import com.antelopesystem.crudframework.crud.cache.adapter.inmemory.InMemoryCacheManagerAdapter
import net.sf.ehcache.Cache
import net.sf.ehcache.CacheManager
import net.sf.ehcache.config.CacheConfiguration
import org.slf4j.LoggerFactory

class EhCacheManagerAdapter(
private val cacheManager: CacheManager
) : CacheManagerAdapter {
override fun getCache(name: String) : CrudCache? {
return CrudEhCacheImpl(cacheManager.getCache(name))
}

override fun createCache(name: String, options: CrudCacheOptions): CrudCache {
log.debug("Attempting to create cache with name [ $name ] and options [ $options ]")
val configuration = CacheConfiguration(name, 0)
val (timeToLiveSeconds, timeToIdleSeconds, maxEntries) = options
if(timeToLiveSeconds != null) {
configuration.timeToLiveSeconds = timeToLiveSeconds
}

if(timeToIdleSeconds != null) {
configuration.timeToIdleSeconds = timeToIdleSeconds
}

if(maxEntries != null) {
configuration.maxEntriesInCache = maxEntries
}

val vendorCache = cacheManager.addCacheIfAbsent(Cache(configuration))
log.debug("Created cache with [ $name ] and options [ $options ]")
return CrudEhCacheImpl(vendorCache)
}

companion object {
private val log = LoggerFactory.getLogger(EhCacheManagerAdapter::class.java)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.antelopesystem.crudframework.crud.cache.adapter.inmemory

import com.antelopesystem.crudframework.crud.cache.CacheManagerAdapter
import com.antelopesystem.crudframework.crud.cache.CrudCache
import com.antelopesystem.crudframework.crud.cache.CrudCacheOptions
import org.slf4j.LoggerFactory

class InMemoryCacheManagerAdapter : CacheManagerAdapter {
private val caches = mutableMapOf<String, CrudCache>()
override fun getCache(name: String) : CrudCache? {
log.debug("Attempting to find cache with name [ $name ]")
val cache = caches[name]
if(cache != null) {
log.debug("Found cache with $name")
} else {
log.debug("Did not find cache with $name")
}

return cache
}

override fun createCache(name: String, options: CrudCacheOptions): CrudCache {
log.debug("Attempting to create or return cache with name [ $name ]")
return caches.computeIfAbsent(name) {
log.debug("Cache with name [ $name ] did not exist, creating")
InMemoryCrudCache()
}
}

companion object {
private val log = LoggerFactory.getLogger(InMemoryCacheManagerAdapter::class.java)
}
}



0 comments on commit f913f34

Please sign in to comment.