Skip to content

Commit

Permalink
#8 Invalidate DBList cache when it is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Feb 13, 2017
1 parent 6e2f292 commit 40df9ec
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/org/elastic4play/models/Attributes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ case class EnumerationAttributeFormat[T <: Enumeration](enum: T)(implicit tag: C
}

case class ListEnumeration(enumerationName: String)(dblists: DBLists) extends AttributeFormat[String](s"enumeration") {
lazy val items = dblists("list_" + enumerationName).cachedItems.map(_.mapTo[String]).toSet //getItems[String].map(_.map(_._2).toSet)
def items = dblists("list_" + enumerationName).cachedItems.map(_.mapTo[String]).toSet //getItems[String].map(_.map(_._2).toSet)
override def checkJson(subNames: Seq[String], value: JsValue) = value match {
case JsString(v) if subNames.isEmpty && items.contains(v) Good(value)
case _ Bad(One(InvalidFormatAttributeError("", name, JsonInputValue(value))))
Expand Down
16 changes: 14 additions & 2 deletions app/org/elastic4play/services/DBList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ class DBListModel(dblistName: String) extends ModelDef[DBListModel, DBListItemEn

class DBListItemEntity(model: DBListModel, attributes: JsObject) extends EntityDef[DBListModel, DBListItemEntity](model, attributes) with DBListItem {
def mapTo[T](implicit reads: Reads[T]) = Json.parse((attributes \ "value").as[String]).as[T]
def dblist = (attributes \ "dblist").as[String]
override def toJson = super.toJson - "value" + ("value" mapTo[JsValue])
}

trait DBListItem {
def id: String
def dblist: String
def mapTo[A](implicit reads: Reads[A]): A
}

Expand All @@ -52,6 +54,7 @@ trait DBList {

@Singleton
class DBLists @Inject() (
getSrv: GetSrv,
findSrv: FindSrv,
deleteSrv: Provider[DeleteSrv],
dbCreate: DBCreate,
Expand All @@ -67,7 +70,13 @@ class DBLists @Inject() (
findSrv(dblistModel, any, groupByField("dblist", selectCount)).map(_.keys)
}

def deleteItem(itemId: String)(implicit authContext: AuthContext) = deleteSrv.get.realDelete[DBListModel, DBListItemEntity](dblistModel, itemId)
def deleteItem(itemId: String)(implicit authContext: AuthContext): Future[Unit] = {
for {
item getSrv[DBListModel, DBListItemEntity](dblistModel, itemId)
_ deleteSrv.get.realDelete[DBListModel, DBListItemEntity](dblistModel, item)
_ = cache.remove(dblistModel.name + "_" + item.dblist)
} yield ()
}

def apply(name: String): DBList = new DBList {
def cachedItems = cache.getOrElse(dblistModel.name + "_" + name, 10.seconds) {
Expand All @@ -90,7 +99,10 @@ class DBLists @Inject() (
val value = Json.toJson(item)
val id = Hasher("MD5").fromString(value.toString).head.toString
dbCreate(dblistModel.name, None, Json.obj("_id" id, "dblist" name, "value" JsString(value.toString)))
.map(dblistModel(_))
.map { newItem
cache.remove(dblistModel.name + "_" + name)
dblistModel(newItem)
}
}
}

Expand Down
14 changes: 8 additions & 6 deletions app/org/elastic4play/services/DeleteSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ class DeleteSrv @Inject() (
}

def realDelete[M <: AbstractModelDef[M, E], E <: EntityDef[M, E]](model: M, id: String)(implicit authContext: AuthContext): Future[Unit] = {
for {
entity getSrv[M, E](model, id)
isFound dbremove(model, entity)
} yield if (isFound)
eventSrv.publish(AuditOperation(entity, AuditableAction.Delete, JsObject(Nil), authContext))
else throw NotFoundError(s"${model.name} $id not found")
getSrv[M, E](model, id).flatMap(entity realDelete(model, entity))
}

def realDelete[M <: AbstractModelDef[M, E], E <: EntityDef[M, E]](model: M, entity: E)(implicit authContext: AuthContext): Future[Unit] = {
dbremove(model, entity).map { isFound
if (isFound) eventSrv.publish(AuditOperation(entity, AuditableAction.Delete, JsObject(Nil), authContext))
else throw NotFoundError(s"${model.name} ${entity.id} not found")
}
}
}
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version := "1.1.2"
version := "1.1.3-SNAPSHOT"

0 comments on commit 40df9ec

Please sign in to comment.