简体   繁体   English

使用memcached使Java Play框架缓存Ebean实体

[英]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. 我正在运行Java Play框架版本v2.6.1并使用Ebean进行持久化。 My intention is to get bean caching going using play2-memcached plugin. 我的目的是使用play2-memcached插件获取bean缓存

What have I done so far? 到目前为止我做了什么?

  • installed memcached on localhost and enabled verbose logging. 在localhost上安装了memcached并启用了详细日志记录。
  • replaced ehcache dependency with cacheApi in libraryDependencies in build.sbt (this, I assume, should remove Ehcache completely). cacheApi中的libraryDependencies中的build.sbt替换了ehcache依赖(我假设,应该完全删除Ehcache)。
  • added "com.github.mumoshu" %% "play2-memcached-play26" % "0.9.0", to libraryDependencies in build.sbt "com.github.mumoshu" %% "play2-memcached-play26" % "0.9.0",libraryDependencies中的build.sbt
  • added "Spy Repository" at "http://files.couchbase.com/maven2", to resolvers in build.sbt "Spy Repository" at "http://files.couchbase.com/maven2",添加"Spy Repository" at "http://files.couchbase.com/maven2",build.sbt resolvers
  • added following entries to application conf: 将以下条目添加到应用程序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. 拿了我的实体并使它实现了Serializable ,还添加了@com.avaje.ebean.annotation.Cache注释。
  • enabled SQL logging 启用S​​QL日志记录

What works? 什么有用?

  • loading entity with Entity.find.byId(id) results SQL SELECT . 使用Entity.find.byId(id)加载实体会导致SQL SELECT Loading it again with different request results no SQL statements. 使用不同的请求结果再次加载它没有SQL语句。
  • opening browser to localhost:11211 shows errors in syslog -- this is to make sure memcached is running and I can see requests appearing 打开浏览器到localhost:11211显示syslog中的错误 - 这是为了确保memcached正在运行,我可以看到请求出现
  • making memory dump I can see that cache related classes from com.github.mumoshu are loaded. 进行内存转储我可以看到com.github.mumoshu中的缓存相关类已加载。

What doesn't work? 什么不起作用?

  • I expect cached objects to be sent to memcached (on read and/or update). 我希望缓存的对象被发送到memcached(在读取和/或更新时)。 This is not happening - there are no memcached logs related to this. 这没有发生 - 没有与此相关的memcached日志。 Neither there are any connections to port 11211 if I run netstat -na | grep 11211 如果我运行netstat -na | grep 11211则没有任何与端口11211的连接 netstat -na | grep 11211 . netstat -na | grep 11211

Is there anything I'm missing? 有什么我想念的吗?

You also need to bind javax.caching.CacheManager . 您还需要绑定javax.caching.CacheManager Add

libraryDependencies += jcache

to your build.sbt . 到你的build.sbt

If you are using Guice, you have to add the following for Java annotations as well: 如果您使用的是Guice,则还必须为Java注释添加以下内容:

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 . 更多信息,请Playframework文档“的JCache支持”一节中找到这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM