Class CachedMongoDBDatabase
java.lang.Object
me.github.simonplays15.betterbansystem.core.database.Database
me.github.simonplays15.betterbansystem.core.database.mongodb.MongoDBDatabase
me.github.simonplays15.betterbansystem.core.database.mongodb.CachedMongoDBDatabase
- All Implemented Interfaces:
IDatabase
A subclass of MongoDBDatabase that implements caching for enhanced performance.
This class maintains a cache of query results to avoid accessing the database unnecessarily.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
createIndex
(String collectionName, String fieldName, boolean unique) Creates an index in the specified collection on the given field.void
Deletes a record from the specified table based on the primary key value.void
Inserts a new document into the specified table in the MongoDB database.void
Executes a query on the MongoDB database and clears the cache.Retrieves a list of rows from a database table based on the given condition.Retrieves all records from the specified table in the MongoDB database.void
update
(String tableName, String primaryKey, Object primaryKeyValue, @NotNull Map<String, Object> newData) Updates a document in the specified MongoDB collection and clears the cache for the updated collection.Methods inherited from class me.github.simonplays15.betterbansystem.core.database.mongodb.MongoDBDatabase
commitTransaction, connect, createDatabaseAndTables, disconnect, executeQuery, rollbackTransaction, startTransaction
-
Constructor Details
-
CachedMongoDBDatabase
public CachedMongoDBDatabase()
-
-
Method Details
-
select
Retrieves a list of rows from a database table based on the given condition. If the result is already present in the cache, it is returned from the cache instead of querying the database.- Specified by:
select
in interfaceIDatabase
- Overrides:
select
in classMongoDBDatabase
- Parameters:
tableName
- the name of the table to select fromcondition
- the condition to use for selecting rows- Returns:
- a list of maps representing the selected rows, where each map contains column names as keys and column values as values
-
selectAll
Retrieves all records from the specified table in the MongoDB database.- Specified by:
selectAll
in interfaceIDatabase
- Overrides:
selectAll
in classMongoDBDatabase
- Parameters:
tableName
- the name of the table to select from- Returns:
- a list of maps representing the selected records
-
insert
Inserts a new document into the specified table in the MongoDB database.- Specified by:
insert
in interfaceIDatabase
- Overrides:
insert
in classMongoDBDatabase
- Parameters:
tableName
- the name of the table to insert the document intodata
- a map containing the data to be inserted, with the field names as keys and the field values as values
-
update
public void update(String tableName, String primaryKey, Object primaryKeyValue, @NotNull @NotNull Map<String, Object> newData) Updates a document in the specified MongoDB collection and clears the cache for the updated collection.- Specified by:
update
in interfaceIDatabase
- Overrides:
update
in classMongoDBDatabase
- Parameters:
tableName
- the name of the collectionprimaryKey
- the name of the primary key fieldprimaryKeyValue
- the value of the primary key for the document to updatenewData
- aMap
of field names and their updated values
-
delete
Deletes a record from the specified table based on the primary key value. This method overrides the delete method in the super class and additionally clears the cache of query results that are associated with the deleted table.- Specified by:
delete
in interfaceIDatabase
- Overrides:
delete
in classMongoDBDatabase
- Parameters:
tableName
- the name of the tableprimaryKey
- the name of the primary key columnprimaryKeyValue
- the value of the primary key to match
-
query
Executes a query on the MongoDB database and clears the cache.- Specified by:
query
in interfaceIDatabase
- Overrides:
query
in classMongoDBDatabase
- Parameters:
queryString
- the query string to execute
-
createIndex
Creates an index in the specified collection on the given field.- Specified by:
createIndex
in interfaceIDatabase
- Overrides:
createIndex
in classMongoDBDatabase
- Parameters:
collectionName
- the name of the collection where the index is to be createdfieldName
- the name of the field on which the index is to be createdunique
- true if the index should enforce a unique constraint, false otherwise
-