caching - Best practice for cache requests on persisted objects in Play/Scala -
I'm trying to exclude caching in my play app, and it looks like it's working, But the code hopes me to get more verbose, and I appreciate some comments on the simplification or something else I remember. This question is a bit of an extension code (the presentation is a bit simplified here):
def findById (id: int): option [lerner] = {cache.get [lerner] ("Learner." + Id) match {no case = & gt; DB.withConnection {built-in C = & gt; // - No cache, db sql ("select from learning *, where id = {id}") .on ('id -> id) .as (learner p.oneOpt) match {case something (learner) = > And gt; {// - Detected, set the cache cache .Set ("learner.", + Learner.id, learner) some (learner)} case _ = & gt; None // - no address in DB, no cache set}} Case (= learner) = & gt; Some (learners) // - Value of return in cache}} EDIT: The learners (in this case) are important to set cash values anywhere or have been set. Installing is necessary, otherwise before joining, a query will set the cache to someone, and then no future AAS or Getallus call will return no matter, even if a value is present.
You can use the getOrElse method like this: cache.get.org else [option] [learner]] ("learner." + Id, end_time) {DB.withConnection {implicit c => // - No Cache, read DB SQL ("ID = ID =" where learning ID = {ID} ") .on ('id - & gt; id) .as (learner p.oneOpt)}} < / Pre> If the option is found in [learning] memory, it is returned immediately, if it is not, then DB is called and its result is terminated. Time is cached with end_time .
Comments
Post a Comment