Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
BigDataViewer_Core_Extension
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
BioinformaticDataCompression
BigDataViewer_Core_Extension
Commits
21a334ef
Commit
21a334ef
authored
8 years ago
by
Tobias Pietzsch
Browse files
Options
Downloads
Patches
Plain Diff
Cache interface and SoftRefCache implementation
parent
e246512e
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/bdv/cache/revised/Cache.java
+30
-0
30 additions, 0 deletions
src/main/java/bdv/cache/revised/Cache.java
src/main/java/bdv/cache/revised/SoftRefCache.java
+193
-0
193 additions, 0 deletions
src/main/java/bdv/cache/revised/SoftRefCache.java
with
223 additions
and
0 deletions
src/main/java/bdv/cache/revised/Cache.java
0 → 100644
+
30
−
0
View file @
21a334ef
package
bdv.cache.revised
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutionException
;
public
interface
Cache
<
K
,
V
>
{
/**
* Returns the value associated with {@code key} in this cache, or
* {@code null} if there is no cached value for {@code key}.
*/
V
getIfPresent
(
Object
key
);
/**
* Returns the value associated with {@code key} in this cache, atomically
* obtaining that value from {@code loader} if necessary. No observable
* state associated with this cache is modified until loading completes.
* This method provides a simple substitute for the conventional
* "if cached, return; otherwise create, cache and return" pattern.
*
* @throws ExecutionException
* if a checked exception was thrown while loading the value
*/
V
get
(
K
key
,
Callable
<
?
extends
V
>
loader
)
throws
ExecutionException
;
// void cleanUp();
// void invalidate( Object key );
// void invalidateAll();
// long size();
}
This diff is collapsed.
Click to expand it.
src/main/java/bdv/cache/revised/SoftRefCache.java
0 → 100644
+
193
−
0
View file @
21a334ef
package
bdv.cache.revised
;
import
java.lang.ref.Reference
;
import
java.lang.ref.ReferenceQueue
;
import
java.lang.ref.SoftReference
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ExecutionException
;
import
java.util.function.Supplier
;
import
com.google.common.util.concurrent.ExecutionError
;
import
com.google.common.util.concurrent.UncheckedExecutionException
;
public
class
SoftRefCache
<
K
,
V
>
implements
Cache
<
K
,
V
>
{
private
final
EntryCache
<
K
,
Entry
>
entryCache
=
new
EntryCache
<>();
final
class
Entry
{
private
V
value
;
public
Entry
()
{
this
.
value
=
null
;
}
public
V
getValue
()
{
return
value
;
}
public
void
setValue
(
final
V
value
)
{
this
.
value
=
value
;
}
}
@Override
public
V
getIfPresent
(
final
Object
key
)
{
final
Entry
entry
=
entryCache
.
get
(
key
);
return
entry
==
null
?
null
:
entry
.
getValue
();
}
@Override
public
V
get
(
final
K
key
,
final
Callable
<
?
extends
V
>
loader
)
throws
ExecutionException
{
final
Entry
entry
=
entryCache
.
computeIfAbsent
(
key
,
()
->
new
Entry
()
);
if
(
entry
.
getValue
()
==
null
)
{
synchronized
(
entry
)
{
if
(
entry
.
getValue
()
==
null
)
{
try
{
entry
.
setValue
(
loader
.
call
()
);
}
catch
(
final
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
throw
new
ExecutionException
(
e
);
}
catch
(
final
RuntimeException
e
)
{
throw
new
UncheckedExecutionException
(
e
);
}
catch
(
final
Exception
e
)
{
throw
new
ExecutionException
(
e
);
}
catch
(
final
Error
e
)
{
throw
new
ExecutionError
(
e
);
}
}
}
}
return
entry
.
getValue
();
}
}
class
EntryCache
<
K
,
V
>
{
final
ConcurrentHashMap
<
K
,
Reference
<
V
>
>
map
=
new
ConcurrentHashMap
<>();
final
ReferenceQueue
<
V
>
queue
=
new
ReferenceQueue
<>();
public
V
get
(
final
Object
key
)
{
final
Reference
<
V
>
ref
=
map
.
get
(
key
);
return
ref
==
null
?
null
:
ref
.
get
();
}
public
V
computeIfAbsent
(
final
K
key
,
final
Supplier
<?
extends
V
>
supplier
)
{
V
value
=
get
(
key
);
if
(
value
==
null
)
{
synchronized
(
this
)
{
value
=
get
(
key
);
if
(
value
==
null
)
{
value
=
supplier
.
get
();
map
.
put
(
key
,
new
CacheSoftReference
(
key
,
value
)
);
}
}
cleanUp
(
10
);
}
return
value
;
}
/**
* Remove references from the cache that have been garbage-collected. To
* avoid long run-times, per call to {@code cleanUp()}, at most
* {@code maxElements} are processed.
*
* @param maxElements
* how many references to clean up at most.
* @return how many references were actually cleaned up.
*/
@SuppressWarnings
(
"unchecked"
)
public
int
cleanUp
(
final
int
maxElements
)
{
int
i
=
0
;
for
(
;
i
<
maxElements
;
++
i
)
{
final
Reference
<
?
>
poll
=
queue
.
poll
();
if
(
poll
==
null
)
break
;
(
(
CacheSoftReference
)
poll
).
clean
();
}
return
i
;
}
class
CacheSoftReference
extends
SoftReference
<
V
>
{
private
final
K
key
;
public
CacheSoftReference
(
final
K
key
,
final
V
referent
)
{
super
(
referent
,
queue
);
this
.
key
=
key
;
}
public
void
clean
()
{
map
.
remove
(
key
,
this
);
}
}
// TODO: remove if not needed:
// public void invalidateAll()
// {
// for ( final Reference< ? > ref : map.values() )
// ref.clear();
// map.clear();
// }
//
// /**
// * Returns the approximate number of entries in this cache.
// */
// public long size()
// {
// return map.size();
// }
//
// public V computeIfAbsent( final K key, final Function<? super K,? extends V> mappingFunction )
// {
// V value = get( key );
// if ( value == null )
// {
// synchronized ( this )
// {
// value = get( key );
// if ( value == null )
// {
// value = mappingFunction.apply( key );
// map.put( key, new CacheSoftReference( key, value ) );
// }
// }
// }
// return value;
// }
//
// public void put( final K key, final V value )
// {
// map.put( key, new CacheSoftReference( key, value ) );
// cleanUp( 10 );
// }
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment