Skip to content

Conversation

dadoonet
Copy link
Contributor

@dadoonet dadoonet commented Oct 18, 2017

There are some use cases where we know we have enough memory and we want to be as fast as possible so we just want to hold in memory a ResultSet related to a given Statement.

This commit implements a new type of cache using memory instead of hard disk files.

It uses a naive implementation with ByteArrayStreams which is not recommended for production.

// Initialize the cache driver
Class.forName("com.qwazr.jdbc.cache.Driver");

// Provide the URL and the Class name of the backend driver
Properties info = new Properties();
info.setProperty("cache.driver.url", "jdbc:derby:memory:myDB;create=true");
info.setProperty("cache.driver.class", "org.apache.derby.jdbc.EmbeddedDriver");
// Get your JDBC connection
Connection cnx = DriverManager.getConnection("jdbc:cache:mem:my-memory-cache", info);

The syntax of the URL can be:

  • jdbc:cache:file:{path-to-the-cache-directory} for on disk cache
  • jdbc:cache:mem:{name-of-the-cache} for in memory cache

There are some use cases where we know we have enough memory
and we want to be as fast as possible so we just want to hold
in memory a ResultSet related to a given Statement.

This commit implements a new type of cache using memory
instead of hard disk files.

It uses a naive implementation with ByteArrayStreams which is not recommended for production.

```java
// Initialize the cache driver
Class.forName("com.qwazr.jdbc.cache.Driver");

// Provide the URL and the Class name of the backend driver
Properties info = new Properties();
info.setProperty("cache.driver.url", "jdbc:derby:memory:myDB;create=true");
info.setProperty("cache.driver.class", "org.apache.derby.jdbc.EmbeddedDriver");
// Get your JDBC connection
Connection cnx = DriverManager.getConnection("jdbc:cache:mem:my-memory-cache", info);
```

The syntax of the URL can be:

* *jdbc:cache:file:{path-to-the-cache-directory}* for on disk cache
* *jdbc:cache:mem:{name-of-the-cache}* for in memory cache
@coveralls
Copy link

Coverage Status

Coverage increased (+1.8%) to 33.598% when pulling 7c3cd12 on dadoonet:pr/add-memory-cache into c75151a on qwazr:master.

@emmanuel-keller emmanuel-keller merged commit 8226a35 into qwazr:master Oct 19, 2017
@emmanuel-keller
Copy link
Contributor

David, thanks for that great patch. Which improvement would make it a production ready feature? I also had in mind using mmap to have both benefits, memory and persistence.

@dadoonet dadoonet deleted the pr/add-memory-cache branch October 19, 2017 10:01
dadoonet added a commit to dadoonet/jdbc-cache-driver that referenced this pull request Oct 19, 2017
No need to keep a stream of bytes in memory as at the end we need
to read it and convert it to an input stream.

Related to qwazr#17.
emmanuel-keller pushed a commit that referenced this pull request Oct 20, 2017
No need to keep a stream of bytes in memory as at the end we need
to read it and convert it to an input stream.

Related to #17.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants