[2.0-RC2] cache api remove object

315 views
Skip to first unread message

zka

unread,
Feb 22, 2012, 4:14:41 PM2/22/12
to play-framework
Hello,

There is no method to remove an object from cache.

For some reason i can not set the expiration to 1 second because it's
too big in my workflow.

is it just an oversight or there is other reason ?

thanks

masahito

unread,
Mar 7, 2012, 9:43:05 PM3/7/12
to play-framework
Hello, all


Please also teach me how to delete an object from Cache!!

https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/play/api/cache/Cache.scala

Why is there nothing?

sun

unread,
Mar 8, 2012, 7:26:29 AM3/8/12
to play-framework
Cache.set("a", null, 0)

中村真人

unread,
Mar 8, 2012, 10:24:25 AM3/8/12
to play-fr...@googlegroups.com
Thanks sun!

2012/3/8 sun <goo...@suncom.de>:


> Cache.set("a", null, 0)
>

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

han.guokai

unread,
May 15, 2012, 3:14:21 PM5/15/12
to play-fr...@googlegroups.com
Cache.set("item.key", null, 0) 
For delete an item in Cache, It doesn't work correctly. In fact, it set value to null ,and never expired !!
Next time, get(key) will return Some(null). And getAs(key) will throw a NullPointerException !!

Because Ehcache allow value is null. And expiration is 0 means never expired
This is too bad.

Or Cache.set("item.key", null, 1) to remove cache after 1's , but it's also bad.
So Cache api should support remove/delete method

This is the current implementation
"""
    def set(key: String, value: Any, expiration: Int) {
      val element = new Element(key, value)
      if (expiration == 0) element.setEternal(true)
      element.setTimeToLive(expiration)
      cache.put(element)
    }
"""

在 2012年3月8日星期四UTC+8下午8时26分29秒,sun写道:

Nilanjan

unread,
May 15, 2012, 6:05:54 PM5/15/12
to play-framework
There is method Cache.remove on trunk right now. But it didn't make it
to version 2.0.1. Here is the reason behind the minimal API design
from the docs:

"The API is intentionally minimal to allow several implementation to
be plugged. If you need a more specific API, use the one provided by
your Cache plugin."

Now if you really have to remove the key from the Cache you can use
the EhCache API to remove the entries from Cache. The default
implementation of the Cache uses EHCache:

import play.api.Play.current
import net.sf.ehcache.Ehcache
current.plugin[Ehcache].map(c => c.remove("key"))

Consider this as a workaround.

Nilanjan

han.guokai

unread,
May 16, 2012, 4:40:47 AM5/16/12
to play-fr...@googlegroups.com
OK.
But the code is : current.plugin[EhCachePlugin].map(c => c.cache.remove(key))

在 2012年5月16日星期三UTC+8上午6时05分54秒,Nilanjan写道:

Will Sargent

unread,
Jun 19, 2012, 12:29:35 AM6/19/12
to play-fr...@googlegroups.com
Close; it's actually:

current.plugin[EhCachePlugin].map {
      ehcache =>
        ehcache.cache.remove(key)
}.getOrElse(false)

I've confirmed it works (and also confirmed the NPE on getAs) and updated the wiki:


Will.
Reply all
Reply to author
Forward
0 new messages