BRCache
Developer(s) | Brandao |
---|---|
Initial release | April 21, 2015 |
Stable release | 1.0 B2
/ 26 January 2016 |
Written in | Java |
Engine | |
Operating system | Cross-platform |
Platform | Java Virtual Machine |
Type | Cache |
License | Apache License 2.0 |
Website | brcache |
Search BRCache on Amazon.
The BRCache is a general-purpose caching system written in Java. It is free and open-source software, licensed under an Apache open source license. It can be used embedded in an application, as a framework, or run in standalone mode.
The org.brandao.brcache package is the base of BRCache. In the package has the class Cache, which has mechanisms to storing and retrieving data.
Getting the BRCache[edit]
Getting the package[edit]
The BRCache team provides release bundles hosted on the SourceForge File Release System, in ZIP. Each release bundle contains JARs, documentation, source code, and other information. You can download releases of BRCache, from the list at http://sourceforge.net/projects/brcache/files/.
Maven Repository Artifacts[edit]
The artifacts are produced under the org.brandao groupId.
brcache
Single artifact, necessary for handling the data in the cache.
The official repository is http://brcache.brandao.org/maven/2.
Overview[edit]
Write and read operations in the cache are made by the class org.brandao.brcache.Cache. Usually only one instance is required for application, but not limited to just one.
Architecture[edit]
A cache is divided into three buffers of limited size. They are called nodes, index and date. Each buffer is divided into pages, and this is subdivided into blocks.
The node buffer is a local for storing the nodes of the tree of the key. Your block size is fixed, 576 bytes. He has to use at least 9% of the total memory used by the cache. Not being less than 2304 bytes (4x576 bytes). The swap factor should be between 0.1 and 0.3. If the swap factor stays above or below the limit, you can leave the slow cache. The swap is made by page.
The index buffer is a local for storing the index of the keys. Your block size is fixed, 82 bytes. He has to use at least 3% of the total memory used by the cache. Not being less than 328 bytes (4x82 bytes). The swap factor should be between 0.1 and 0.3. If the swap factor stays above or below the limit, you can leave the slow cache. The swap is made by page.
The data buffer is a local for storing the values. Your block size is variable and can be configured. He has to use at least 88% of the total memory used by the cache. The swap factor should be between 0.1 and 0.3. If the swap factor stays above or below the limit, you can leave the slow cache. The swap is made by page.
The cache[edit]
A cache is represented by the class org.brandao.brcache.Cache. Usually only one instance is required for application, but not limited to just one. Each cache instance need a limited quantity of memory that can be configured.
public class Cache implements Serializable{
public void putObject(String key, long maxAliveTime, Object item)
throws StorageException{...}
public Object getObject(String key) throws RecoverException{...}
public void put(String key, long maxAliveTime, InputStream inputData)
throws StorageException{...}
public InputStream get(String key) throws RecoverException{...}
public boolean remove(String key) throws RecoverException{...}
public long getCountRead(){...}
public long getCountWrite(){...}
public long getCountRemoved() {...}
public long getCountReadData() {...}
public long getCountWriteData() {...}
public long getCountRemovedData() {...}
}
- putObject(): Adds or overrides an entity in the cache.
- getObject(): Retrieve an entity from the cache. If not exist, returns null.
- put(): Adds or override an entity in the cache from a byte stream.
- get(): Retrieves a byte stream from the cache. If not exist, returns null.
- remove(): Remove an entity from the cache.
- getCountRead(): Gets the number of recovered items.
- getCountWrite(): Gets the number of stored items.
- getCountRemoved(): Gets the number of items removed.
- getCountReadData(): Gets the number of bytes recovered.
- getCountWriteData(): Gets the number of stored bytes.
- getCountRemovedData(): Gets the number of bytes removed
Storing items in the cache[edit]
The storing item in a cache can be done by method putObject() or put(), and exists a limit to the size of the key and value.
The item can be removed after a certain time, if inform the maxAliveTime. If maxAliveTime equals zero, the entry stays forever in the cache.
The method putObject() stores a particular object, but before storing, it is converted a byte stream, that is to say, the object must implement the java.io.Serializable interface.
String key = "myKey";
String value = "Valor";
cache.putObject(key, 0, value);
The put() method stores a stream of bytes.
String key = "myKey";
String value = "Valor";
//convert object to byte stream.
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(item);
oout.flush();
//stores the byte stream.
cache.put(key, 0, new ByteArrayInputStream(bout.toByteArray()));
Retrieving items from the cache[edit]
The recovery items from the cache can be done by the getObject or get. The method getObject gets the item data and converts them into an object.
String key = "myKey";
String value = (String)cache.getObject(key);
The method get return the byte stream of the item. To get the object, you must convert the byte stream into object.
String key = "myKey";
InputStream valueStream = cache.get(key, value);
ObjectInputStream in = new ObjectInputStream(valueStream);
String value = (String)in.readObject();
Removing items from the cache[edit]
The removal items from the cache can be done by the remove() method. If the item is removed from the cache, it will return true. Otherwise, it returns false.
String key = "myKey";
if(cache.remove(key)){
System.out.println(“O item ” + key + “ foi removido do cache”).
}
else{
System.out.println(“O item ” + key + “ não existe no cache”).
}
BRCache server[edit]
The BRCache server is a cache that allows storing key/value pairs. It can be used in any application. Supports storing in memory and disk.
To start the server, execute the below command:
<java> -Xmx<mem> -cp brcache.jar org.brandao.brcache.server.Main
- java: jvm.
- mem: maximum memory allocation pool for a Java Virtual Machine. The value should be greater than the cache consumer.
Configuration[edit]
In the startup the BRCache attempts to read the brcache.conf file that should be located on the same server run folder. It must contain the following variables:
- port: port that the server will listen. The default value is 8084.
- max_connections: maximum simultaneous connections that are allowed. The default value is 1024.
- swapper_thread: number of swapper threads.
- timeout_connection: timeout connection. The default value is 0.
- reuse_address: allow reuse the allocated connections. The default value is false.
- compress_stream: enable or disable data compression. The default value is false.
- data_path: folder where the server will do the swap data when the memory limit is reached. The default value is /var/brcache.
- nodes_buffer_size: node buffer size. The default value is 16m.
- nodes_page_size: node buffer page size. The default value is 16k.
- nodes_swap_factor: node swap factor. O valor padrão é 0.3.
- index_buffer_size: index buffer size. The default value is 1m.
- index_page_size: index buffer page size. The default value is 16k.
- index_swap_factor: index swap factor. The default value is 0.3.
- data_buffer_size: data buffer size. The default value is 64m
- data_page_size: data buffer page size. The default value is 16k.
- data_block_size: data block size. The default value is 512 bytes.
- data_swap_factor: data swap factor. The default value is 0.3.
- write_buffer_size: write buffer size. The default value is 16k.
- read_buffer_size: read buffer size. The default value is 16k.
- max_size_entry: max size item in bytes. The default value is 1m.
- max_size_key: max key size in bytes. The default value is 48.
Storage Command[edit]
The storage command is put. The syntax of it is:
put <key> <flags> <exptime> <size>\r\n
<data>\r\n
END\r\n
- key: is the key associated with data.
- flags: is an arbitrary 16-bit unsigned integer. It will be recovered with the value.
- exptime: is expiration time. If it's 0, the item never expires.
- size: is the number of bytes in the data without the delimiting \r\n.
- data: is the data of the item. The length is defined in the size.
if command succeeded, the server returns the message OK\r\n. Otherwise the server returns the error message.
Retrieval command[edit]
The retrieval command is get. The syntax of it is:
get <key>\r\n
- key: is the key associated with data.
if command succeeded, the server returns the message below. Otherwise the server returns the error message.
VALUE <key> <size> <flags>\r\n
<data>\r\n
END\r\n
- key: is the key associated with data.
- flags: is an arbitrary 16-bit unsigned integer informed in the storage command.
- size: is the number of bytes in the data without the delimiting \r\n.
- data: is the data of the item.
Deletion command[edit]
The deletion command is remove. The syntax of it is:
remove <key>\r\n
- key: is the key associated with data.
if command succeeded, the server returns the message OK\r\n. Otherwise the server returns the error message.
External links[edit]
This article "BRCache" is from Wikipedia. The list of its authors can be seen in its historical. Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.