All Implemented Interfaces:
IDatabase

public class CachedMongoDBDatabase extends MongoDBDatabase
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 Details

    • CachedMongoDBDatabase

      public CachedMongoDBDatabase()
  • Method Details

    • select

      public List<Map<String,Object>> select (String tableName, String condition)
      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 interface IDatabase
      Overrides:
      select in class MongoDBDatabase
      Parameters:
      tableName - the name of the table to select from
      condition - 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

      public List<Map<String,Object>> selectAll (String tableName)
      Retrieves all records from the specified table in the MongoDB database.
      Specified by:
      selectAll in interface IDatabase
      Overrides:
      selectAll in class MongoDBDatabase
      Parameters:
      tableName - the name of the table to select from
      Returns:
      a list of maps representing the selected records
    • insert

      public void insert (String tableName, @NotNull @NotNull Map<String,Object> data)
      Inserts a new document into the specified table in the MongoDB database.
      Specified by:
      insert in interface IDatabase
      Overrides:
      insert in class MongoDBDatabase
      Parameters:
      tableName - the name of the table to insert the document into
      data - 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 interface IDatabase
      Overrides:
      update in class MongoDBDatabase
      Parameters:
      tableName - the name of the collection
      primaryKey - the name of the primary key field
      primaryKeyValue - the value of the primary key for the document to update
      newData - a Map of field names and their updated values
    • delete

      public void delete (String tableName, String primaryKey, Object primaryKeyValue)
      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 interface IDatabase
      Overrides:
      delete in class MongoDBDatabase
      Parameters:
      tableName - the name of the table
      primaryKey - the name of the primary key column
      primaryKeyValue - the value of the primary key to match
    • query

      public void query (String queryString)
      Executes a query on the MongoDB database and clears the cache.
      Specified by:
      query in interface IDatabase
      Overrides:
      query in class MongoDBDatabase
      Parameters:
      queryString - the query string to execute
    • createIndex

      public void createIndex (String collectionName, String fieldName, boolean unique)
      Creates an index in the specified collection on the given field.
      Specified by:
      createIndex in interface IDatabase
      Overrides:
      createIndex in class MongoDBDatabase
      Parameters:
      collectionName - the name of the collection where the index is to be created
      fieldName - the name of the field on which the index is to be created
      unique - true if the index should enforce a unique constraint, false otherwise