Cache Persistence Manager Factory

This tutorial will guide you through setting up an embedded Onyx In-Memory Database, configuring the connection using the Cache Persistence Manager Factory, and performing basic operations.

Note: Cloud vs. Open Source Implementation

This guide focuses on the Onyx Open Source Database. In contrast, the Onyx Cloud Database offers managed services where persistence managers are encapsulated, eliminating the need for manual configuration. For more details on the cloud implementation, refer to our comprehensiveUser Guide for Onyx Cloud Database.

The cloud database provides additional features such as enhanced security, scalability, and automated backups.

Download Example Code on GitHub

Why Use Cache Persistence Manager Factory?

The Cache Persistence Manager Factory is used to configure your In-Memory Database with the Onyx Cache. The cache compresses and indexes your data, allowing you to store and access entities efficiently using off-heap resources, while benefiting from all Onyx Database ORM features.

Note: It is recommended to create a global singleton instance of the factory, rather than constructing a new manager every time it's needed.

Example: Using CachePersistenceManagerFactory

Below is an example of how to set up the CachePersistenceManagerFactory in both Kotlin and Java.

1

Add Gradle Dependency

To use the Cache Persistence Manager Factory, you first need to add the Onyx Database dependencies to your project. Choose the appropriate Gradle snippet based on your build script (Kotlin DSL or Groovy DSL) below.
1repositories {
2    mavenCentral()
3    maven {
4        url = uri("https://maven.pkg.github.com/OnyxDevTools/onyx-database-parent")
5        credentials {
6            username = "github_username" // Replace with your GitHub username
7            password = "github_access_token" // Replace with your GitHub access token
8        }
9    }
10}
11
12dependencies {
13    implementation("com.onyxdevtools:onyx-database:3.4.4") // Embedded Database
14    implementation("com.onyxdevtools:onyx-remote-database:3.4.4") // Remote Database Server
15    implementation("com.onyxdevtools:onyx-remote-driver:3.4.4") // Remote Database Client
16}
2

Create an Instance of CacheManagerFactory

Initialize the factory to set up the in-memory database with the desired configuration.
1val factory = CacheManagerFactory()
2factory.initialize()
3
4val manager = factory.persistenceManager
3

Get the PersistenceManager from the Factory

Use the PersistenceManager to perform CRUD operations on your in-memory entities.
4

Save, Query, Update, and Delete Entities

With the PersistenceManager, you can perform all CRUD operations on entities in memory.
5

Close the Cache Manager Factory

Always manually close the factory when you're finished to clear used memory, although a JVM shutdown hook will automatically close it upon termination.
1factory.close()

Troubleshooting

  • OutOfMemoryException: Ensure you have enough memory in order to support the CacheManagerFactory.

Next Steps

Congratulations! You've successfully set up an embedded Onyx Cache Database. To further enhance your knowledge, proceed to the next tutorials: