class
RestoringWeakReference[T <: AnyRef] extends AnyRef
Instance Constructors
-
new
RestoringWeakReference(reference: WeakReference[T], restorer: () ⇒ T)
Value Members
-
final
def
!=(arg0: AnyRef): Boolean
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: AnyRef): Boolean
-
final
def
==(arg0: Any): Boolean
-
def
apply(): T
-
final
def
asInstanceOf[T0]: T0
-
def
clone(): AnyRef
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
def
toString(): String
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
RestoringWeakReference
contains ascala.ref.WeakReference
that, after it has been nulled out, uses a restorer function to restore the value. This can be used for data that can afford to be evicted by the garbage collector, but will be needed later. One good example is Lift form callbacks, which may need the value of an object, but where you don't necessarily want to be retaining the object indefinitely while someone is on a page in the face of GC contention.You can use
RestoringWeakReference
in a couple of basic ways:In this situation, the
RestoringWeakReference
will immediately call the passed function to provide the starting value for the reference, and then the same function will be used if the reference is evicted.Here, starter is an
Option[User]
andUser.find
returns anOption[User]
. The first parameter will be used to initialize the weak reference, while the function will be used to restore the value if the reference is evicted.If you already have a
WeakReference
instance, you can instantiateRestoringWeakReference
directly and pass that reference as the starting value, as well.In all these cases, you use
ref.value
to get a hold of the value. That function will return the value if it is available, and, if not, it will restore the WeakReference and then return the value.