Class CachedSearchService

java.lang.Object
com.university.bookstore.search.CachedSearchService

public class CachedSearchService extends Object
Service that integrates Trie-based prefix search with LRU caching for optimal performance. Provides fast prefix-based material searching with intelligent result caching.

This service combines the efficiency of Trie data structures for prefix searching with LRU caching to avoid repeated computation for frequently accessed queries.

Since:
2024-09-15
Version:
3.0
Author:
Navid Mohaghegh
  • Constructor Details

    • CachedSearchService

      public CachedSearchService(MaterialRepository repository, int cacheSize)
      Creates a new cached search service.
      Parameters:
      repository - the material repository to search
      cacheSize - the maximum number of cached search results
  • Method Details

    • searchByPrefix

      public List<Material> searchByPrefix(String prefix)
      Searches for materials by title prefix with caching.
      Parameters:
      prefix - the title prefix to search for
      Returns:
      list of materials matching the prefix
    • searchByPrefixWithLimit

      public List<Material> searchByPrefixWithLimit(String prefix, int limit)
      Searches for materials by title prefix with result limit and caching.
      Parameters:
      prefix - the title prefix to search for
      limit - the maximum number of results to return
      Returns:
      list of materials matching the prefix, limited to the specified count
    • addMaterial

      public void addMaterial(Material material)
      Adds a material to the search index and invalidates relevant cache entries.
      Parameters:
      material - the material to add
    • removeMaterial

      public void removeMaterial(Material material)
      Removes a material from the search index and invalidates relevant cache entries.
      Parameters:
      material - the material to remove
    • refreshIndex

      public void refreshIndex()
      Refreshes the search index from the repository. Clears the cache to ensure consistency.
    • getCacheStats

      public SearchResultCache.CacheStats getCacheStats()
      Gets cache statistics for performance monitoring.
      Returns:
      cache statistics
    • getIndexSize

      public int getIndexSize()
      Gets the current size of the search index.
      Returns:
      the number of materials in the index
    • isIndexEmpty

      public boolean isIndexEmpty()
      Checks if the search index is empty.
      Returns:
      true if no materials are indexed
    • clear

      public void clear()
      Clears the search index and cache.
    • toString

      public String toString()
      Overrides:
      toString in class Object