Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java library the can cache data in memory or disk depending on the size of the data?

Is the a way to write something to a buffer, but once the buffer gets to large, start writing to a file?

I would like this whole process to be automatic. Basically, I would like to write to a buffer, and have the library make sure I don't run out of memory.

I never want to block the writing process. If the buffer gets too large the library should write some of it to disk.

Does anyone know a library like that?

Grae

The main point is that I only want to write to disk when I have to. The data should be kept in memory if buffer size permits.

like image 913
GC_ Avatar asked Sep 06 '11 15:09

GC_


People also ask

Does Java have a cache?

JCache API (JSR 107)JCache is a Java API for caching. It provides a set of common interfaces and classes that can be used to temporarily store Java objects in memory. It is a JCP standard represented by JSR 107.

What is Java in memory cache?

The Java Object Cache is an in-process, process-wide caching service for general application use. That is, objects are cached within the process memory space, and the Java Object Cache is a single service that is shared by all threads running in the process, in contrast to a service that runs in another process.

Where is cached data stored in Java?

Memory objects are Java objects that the Java Object Cache manages. Memory objects are stored in the Java VM's heap space as Java objects.

What are the different caching techniques available in Java?

There are four types of caching are as follows: In-memory Caching. Database Caching. Web server Caching.


2 Answers

The Heritix project includes a class called RecordingOutputStream. From the javadocs:

The RecordingOutputStream uses an in-memory buffer and backing disk file to allow it to record streams of arbitrary length limited only by available disk space.

As long as the stream recorded is smaller than the in-memory buffer, no disk access will occur.

You can take a look at the source code to find out how this is achieved.

like image 171
Brandon E Taylor Avatar answered Nov 08 '22 09:11

Brandon E Taylor


EHCache does exactly that. All you need to do is configure it so that when it holds a certain limit of objects, they'll be automatically flushed to the disk storage

like image 40
Kico Lobo Avatar answered Nov 08 '22 08:11

Kico Lobo