Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Write-to-disk cache via WeakReferences?

Reply
Thread Tools

Re: Write-to-disk cache via WeakReferences?

 
 
Paul J. Lucas
Guest
Posts: n/a
 
      08-03-2005
Thomas Hawtin <(E-Mail Removed)> wrote:

> The situation in code looks something like this (only with better names):
>
> public class Resource {
> public void doStuff() { ... }
> }
> public class Proxy {
> private final Resource target;
> Proxy(Resource target) {
> if (target == null) {
> throw new NullPointerException();
> }
> this.target = target;
> }
> public void doStuff() {
> target.doStuff();
> }
> }
> class Ref extends Reference<Proxy> {
> private final Resource resource;
> public Ref(Proxy proxy, Resource resource) {
> super(proxy);
> this.resource = resource;
> }
> Resource getResource() {
> return resource;
> }
> }
> class Saver implements Runnable {
> ...
> public void run() {
> for (; {
> Ref ref = (Ref)queue.remove();
> saveToDisk(ref.getResource());
> }
> }
> }


Where did the SoftReference go?
You never create/pass a ReferenceQueue anywhere.
What about my code below?

- Paul


public interface StrongReference {
Object getAndClearStrong();
}

public class SoftStrongReference extends SoftReference
implements StrongReference {

public SoftStrongReference( Object referrent, ReferenceQueue refQ ) {
super( new Sacrifice(), refQ );
m_referrent = referrent;
}

public Object getAndClearStrong() {
final Object temp = m_referrent;
m_referrent = null;
return temp;
}

private static final class Sacrifice {
}

private Object m_referrent;
}

public interface ObjectSaver {
void save( Object obj );
}

public class ReferenceQueueManager extends Thread {

public ReferenceQueueManager( ObjectSaver saver ) {
setDaemon( true );
m_saver = saver;
m_refQ = new ReferenceQueue();
}

public void kill() {
m_killed = true;
interrupt();
}

public void run() {
while ( !m_killed ) {
try {
final StrongReference ref = (StrongReference)m_refQ.remove();
m_saver.save( ref.getAndClearStrong() );
}
catch ( InterruptedException e ) {
// do nothing
}
}
}

private boolean m_killed;
private final ObjectSaver m_saver;
private final ReferenceQueue m_refQ;
}
 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      08-03-2005
Paul J. Lucas wrote:
> Thomas Hawtin <(E-Mail Removed)> wrote:
>
>
>>The situation in code looks something like this (only with better names):
>>
>>public class Resource {
>> public void doStuff() { ... }
>>}
>>public class Proxy {
>> private final Resource target;
>> Proxy(Resource target) {
>> if (target == null) {
>> throw new NullPointerException();
>> }
>> this.target = target;
>> }
>> public void doStuff() {
>> target.doStuff();
>> }
>>}
>>class Ref extends Reference<Proxy> {

^^^^^^^^^SoftReference
>> private final Resource resource;
>> public Ref(Proxy proxy, Resource resource) {

^, ReferenceQueue queue
>> super(proxy);

^, queue
>> this.resource = resource;
>> }
>> Resource getResource() {
>> return resource;
>> }
>>}
>>class Saver implements Runnable {
>> ...
>> public void run() {
>> for (; {
>> Ref ref = (Ref)queue.remove();
>> saveToDisk(ref.getResource());
>> }
>> }
>>}

>


> Where did the SoftReference go?
> You never create/pass a ReferenceQueue anywhere.


Typod out (more or less).

> public class SoftStrongReference extends SoftReference
> implements StrongReference {
>
> public SoftStrongReference( Object referrent, ReferenceQueue refQ ) {
> super( new Sacrifice(), refQ );
> m_referrent = referrent;
> }


> final StrongReference ref = (StrongReference)m_refQ.remove();
> m_saver.save( ref.getAndClearStrong() );


Sacrifice is up for GC immediately. Therefore if you're still using the
resource it may be persisted whilst still in use.

Neither code refereshes the soft reference, so the GC will treat it as
if it is unused. In my code Proxy could hold a (strong) reference to
Ref<Proxy> and prod it from time to time..

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
 
Joan
Guest
Posts: n/a
 
      08-03-2005

"Thomas Hawtin" <(E-Mail Removed)> wrote in message
news:42f10a79$0$14660$(E-Mail Removed)...
> Paul J. Lucas wrote:
>> Thomas Hawtin <(E-Mail Removed)> wrote:
>>
>>
>>>The situation in code looks something like this (only with
>>>better names):
>>>
>>>public class Resource {

<snip>

I don't quite see the value of this technique. Suppose you get a
handle
to an Integer object. It's just a number. You have no idea what
the
significance is.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET Cache vs Window System Cache Sergey via DotNetMonster.com ASP .Net 0 11-15-2006 03:33 PM
no-cache - need to reload page from server and not the cache John HTML 2 10-29-2006 05:35 PM
client-side cache vs server-side cache vs ajax vs asp.net callback =?Utf-8?B?b25l?= ASP .Net 1 03-08-2006 12:25 PM
Page.Cache vs HttpContext.Current.Cache DesignerX ASP .Net 5 01-20-2004 10:55 PM
Cache::Cache Stale Segments Jeff Nokes Perl 0 09-30-2003 04:34 PM



Advertisments