Skip to content

Commit

Permalink
Add ability to create cache if missing via CachedBy (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed Apr 8, 2021
1 parent b8bfed3 commit 936e7e7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
package com.antelopesystem.crudframework.crud.annotation

@Target(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS)
annotation class CachedBy(val value: String)
annotation class CachedBy(
/**
* The name of the cache
*/
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
Expand Up @@ -12,6 +12,7 @@
import com.antelopesystem.crudframework.crud.exception.CrudTransformationException;
import com.antelopesystem.crudframework.crud.exception.CrudValidationException;
import com.antelopesystem.crudframework.crud.hooks.interfaces.CRUDHooks;
import com.antelopesystem.crudframework.crud.model.EntityCacheMetadata;
import com.antelopesystem.crudframework.crud.model.EntityMetadataDTO;
import com.antelopesystem.crudframework.exception.WrapException;
import com.antelopesystem.crudframework.exception.dto.ErrorField;
Expand Down Expand Up @@ -348,16 +349,21 @@ public <ID extends Serializable, Entity extends BaseCrudEntity<ID>> CrudCache ge
}

EntityMetadataDTO dto = getEntityMetadata(clazz);
if(dto.getCacheName() == null) {
EntityCacheMetadata cacheMetadata = dto.getCacheMetadata();
if(cacheMetadata == null) {
cacheMap.put(clazz.getName(), null);
return null;
}

CrudCache cache = cacheManagerAdapter.getCache(dto.getCacheName());
CrudCache cache = cacheManagerAdapter.getCache(cacheMetadata.getName());
if(cache == null) {
throw new CrudException("Cache for entity [ " + clazz.getSimpleName() + " ] with name [ " + dto.getCacheName() + " ] not found");
}
if(cacheMetadata.getCreateIfMissing()) {
cache = cacheManagerAdapter.createCache(cacheMetadata.getName(), cacheMetadata.getOptions());
} else {
throw new CrudException("Cache for entity [ " + clazz.getSimpleName() + " ] with name [ " + dto.getCacheMetadata().getName() + " ] not found");
}

}
cacheMap.put(clazz.getName(), cache);

return cache;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.antelopesystem.crudframework.crud.model

import com.antelopesystem.crudframework.crud.annotation.*
import com.antelopesystem.crudframework.crud.cache.CrudCacheOptions
import com.antelopesystem.crudframework.crud.handler.CrudDao
import com.antelopesystem.crudframework.crud.hooks.interfaces.CRUDHooks
import com.antelopesystem.crudframework.model.BaseCrudEntity
Expand All @@ -15,13 +16,13 @@ import kotlin.reflect.full.allSuperclasses

class EntityMetadataDTO {

val simpleName: String;
val simpleName: String

val deleteField: Field?

val deleteableType: DeleteableType

val cacheName: String?
val cacheMetadata: EntityCacheMetadata?

val immutable: Boolean

Expand All @@ -40,7 +41,7 @@ class EntityMetadataDTO {
constructor(entityClazz: Class<out BaseCrudEntity<*>>) {
deleteField = getEntityDeleteField(entityClazz)
deleteableType = getEntityDeleteableType(entityClazz)
cacheName = getEntityCacheName(entityClazz)
cacheMetadata = getEntityCacheMetadata(entityClazz)
immutable = isEntityImmutable(entityClazz)
alwaysPersistCopy = shouldAlwaysPersistCopy(entityClazz)
collectHookAnnotations(entityClazz)
Expand Down Expand Up @@ -114,9 +115,23 @@ class EntityMetadataDTO {
return crudEntity.dao.java
}

private fun getEntityCacheName(clazz: Class<out BaseCrudEntity<*>>): String? {
val cachedBy = clazz.getDeclaredAnnotation(CachedBy::class.java)
return cachedBy?.value
private fun getEntityCacheMetadata(clazz: Class<out BaseCrudEntity<*>>): EntityCacheMetadata? {
val cachedBy = clazz.getDeclaredAnnotation(CachedBy::class.java) ?: return null
fun Long.nullIfMinusOne(): Long? = if(this == -1L) {
null
} else {
this
}

return EntityCacheMetadata(
cachedBy.value,
cachedBy.createIfMissing,
CrudCacheOptions(
cachedBy.timeToLiveSeconds.nullIfMinusOne(),
cachedBy.timeToIdleSeconds.nullIfMinusOne(),
cachedBy.maxEntries.nullIfMinusOne()
)
)
}

private fun getEntityDeleteableType(clazz: Class<out BaseCrudEntity<*>>): DeleteableType {
Expand Down Expand Up @@ -156,4 +171,10 @@ class EntityMetadataDTO {
companion object {
private const val MAX_FILTERFIELD_DEPTH = 4
}
}
}

data class EntityCacheMetadata(
val name: String,
val createIfMissing: Boolean,
val options: CrudCacheOptions
)

0 comments on commit 936e7e7

Please sign in to comment.