Skip to content
Robert Gaggl edited this page Feb 27, 2016 · 6 revisions

Entity cache

SQLstore features an entity cache to avoid issuing multiple SQL statements for the same objects. The entity cache (aka object cache) is an LRU type which has a default size of 1000 objects. Every entity retrieved, be it when resolving an object mapping, populating a collection or when manually querying using query(), is put into this cache.

If using Ringo SQLstore in a multithreaded environment (which is the case for most RingoJS applications), all caches should be instantiated as singletons. After instantiation, the cache can be put into action by calling the store's setEntityCache() method:

var {Store, Cache} = require("ringo-sqlstore");
var entityCache = module.singleton("entityCache", function() {
    // the argument defines the cache size
    return new Cache(1000);
});
var connectionPool = module.singleton("connectionCache", function() {
    return new ConnectionPool({
        "url": "jdbc:h2:mem:test",
        "driver": "org.h2.Driver"
    });
});
var store = new Store(connectionPool);
store.setEntityCache(entityCache);

This page explains in detail how SQLstore retrieves values received from the database and utilizes the object cache.

Query cache

Ringo SQLstore can use a second cache for performance gains - the query cache. If defined all queries, regardless whether specified in mapping definitions or in store.query(…) calls, are parsed only once and put into this cache. As with the entity cache, it should be instantiated as a singleton:

var {Store, ConnectionPool, Cache} = require("ringo-sqlstore");
var queryCache = module.singleton("queryCache", function() {
    // the argument defines the cache size
    return new Cache(1000);
});
var connectionPool = module.singleton("connectionCache", function() {
    return new ConnectionPool({
        "url": "jdbc:h2:mem:test",
        "driver": "org.h2.Driver"
    });
});
var store = new Store(connectionPool);
store.setQueryCache(queryCache);

Query processing and the effect of the query cache is explained in detail on this page.

Clone this wiki locally