Permalink
Browse files

Add RestoringWeakReference to utils.

RestoringWeakReference contains a scala.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.
  • Loading branch information...
1 parent 731c4dc commit 5833892cd9e9704574609c913b1d7a84b768fef3 @Shadowfiend Shadowfiend committed Nov 9, 2012
Showing with 83 additions and 0 deletions.
  1. +83 −0 core/util/src/main/scala/net/liftweb/util/RestoringWeakReference.scala
View
83 core/util/src/main/scala/net/liftweb/util/RestoringWeakReference.scala
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2006-2011 WorldWide Conferencing, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.liftweb
+package util
+
+import scala.ref.WeakReference
+
+/**
+ * `RestoringWeakReference` contains a `scala.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:
+ *
+ * {{{
+ * val ref = RestoringWeakReference(() => User.find(id))
+ * }}}
+ * 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.
+ *
+ * {{{
+ * val ref = RestoringWeakReference(starter, () => User.find(id))
+ * }}}
+ * Here, starter is an `Option[User]` and `User.find` returns an
+ * `Option[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.
+ *
+ * {{{
+ * val baseRef = new WeakReference(starter)
+ * val ref = new RestoringWeakReference(baseRef, () => User.find(id))
+ * }}}
+ * If you already have a `WeakReference` instance, you can instantiate
+ * `RestoringWeakReference` 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.
+ */
+class RestoringWeakReference[T <: AnyRef](private var reference:WeakReference[T], restorer:()=>T) {
+ def value : T = {
+ val existing = reference.get
+ if (! existing.isDefined) {
+ restoreReference
+ value
+ } else {
+ existing.get
+ }
+ }
+
+ private def restoreReference = {
+ reference = new WeakReference(restorer())
+ }
+}
+object RestoringWeakReference {
+ def apply[T <: AnyRef](restorer:()=>T) = {
+ new RestoringWeakReference(new WeakReference(restorer()), restorer)
+ }
+ def apply[T <: AnyRef](starter:T, restorer:()=>T) = {
+ new RestoringWeakReference(new WeakReference(starter), restorer)
+ }
+}
+

4 comments on commit 5833892

@fmpwizard
Lift Web Framework member

Thanks for adding this, any chance to get a spec test with the pull req?

@nafg

Looks nice.
It might be more in line with scala conventions to rename value to apply. Then you would write ref() instead of ref.value.

P.S. Would you you mind to fix the indentation?
Thanks.

@Shadowfiend
Lift Web Framework member

Wow. No idea what happened to the indentation there. I'll definitely fix that.

As for apply vs value… Let me ponder that one for a bit.

@Shadowfiend
Lift Web Framework member

Indeed, looks like WeakReference uses apply, no reason why we shouldn't do the same. Pushed that change to master.

Please sign in to comment.