Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Java Play framework to cache Ebean entities using memcached

I am running Java Play framework version v2.6.1 and using Ebean for persistence. My intention is to get bean caching going using play2-memcached plugin.

What have I done so far?

  • installed memcached on localhost and enabled verbose logging.
  • replaced ehcache dependency with cacheApi in libraryDependencies in build.sbt (this, I assume, should remove Ehcache completely).
  • added "com.github.mumoshu" %% "play2-memcached-play26" % "0.9.0", to libraryDependencies in build.sbt
  • added "Spy Repository" at "http://files.couchbase.com/maven2", to resolvers in build.sbt
  • added following entries to application conf:

play.modules.disabled += "play.api.cache.ehcache.EhCacheModule" play.modules.enabled+="com.github.mumoshu.play2.memcached.MemcachedModule" play.cache.defaultCache=default play.cache.bindCaches=["db-cache", "user-cache", "session-cache"] memcached.host="127.0.0.1:11211"

  • took my entity and made it implement Serializable, also added @com.avaje.ebean.annotation.Cache annotation.
  • enabled SQL logging

What works?

  • loading entity with Entity.find.byId(id) results SQL SELECT. Loading it again with different request results no SQL statements.
  • opening browser to localhost:11211 shows errors in syslog -- this is to make sure memcached is running and I can see requests appearing
  • making memory dump I can see that cache related classes from com.github.mumoshu are loaded.

What doesn't work?

  • I expect cached objects to be sent to memcached (on read and/or update). This is not happening - there are no memcached logs related to this. Neither there are any connections to port 11211 if I run netstat -na | grep 11211.

Is there anything I'm missing?

like image 630
mindas Avatar asked Aug 22 '17 17:08

mindas


1 Answers

You also need to bind javax.caching.CacheManager. Add

libraryDependencies += jcache

to your build.sbt.

If you are using Guice, you have to add the following for Java annotations as well:

libraryDependencies += "org.jsr107.ri" % "cache-annotations-ri-guice" % "1.0.0"

More information can be found in the "JCache Support" section of the Playframework documentation here.

like image 133
mkurz Avatar answered Oct 19 '22 04:10

mkurz