Class CachedSearchService
java.lang.Object
com.university.bookstore.search.CachedSearchService
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 Summary
ConstructorsConstructorDescriptionCachedSearchService(MaterialRepository repository, int cacheSize) Creates a new cached search service. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMaterial(Material material) Adds a material to the search index and invalidates relevant cache entries.voidclear()Clears the search index and cache.Gets cache statistics for performance monitoring.intGets the current size of the search index.booleanChecks if the search index is empty.voidRefreshes the search index from the repository.voidremoveMaterial(Material material) Removes a material from the search index and invalidates relevant cache entries.searchByPrefix(String prefix) Searches for materials by title prefix with caching.searchByPrefixWithLimit(String prefix, int limit) Searches for materials by title prefix with result limit and caching.toString()
-
Constructor Details
-
CachedSearchService
Creates a new cached search service.- Parameters:
repository- the material repository to searchcacheSize- the maximum number of cached search results
-
-
Method Details
-
searchByPrefix
-
searchByPrefixWithLimit
Searches for materials by title prefix with result limit and caching.- Parameters:
prefix- the title prefix to search forlimit- the maximum number of results to return- Returns:
- list of materials matching the prefix, limited to the specified count
-
addMaterial
Adds a material to the search index and invalidates relevant cache entries.- Parameters:
material- the material to add
-
removeMaterial
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
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
-