Skip to main content

ri_memory_cache_new

Function ri_memory_cache_new 

Source
#[unsafe(no_mangle)]
pub extern "C" fn ri_memory_cache_new() -> *mut CRiMemoryCache
Expand description

Creates a new RiMemoryCache instance.

Initializes an empty in-memory cache with default configuration. The cache starts empty and grows as entries are added. Memory usage is managed automatically through eviction policies.

§Returns

Pointer to newly allocated RiMemoryCache on success, or NULL if memory allocation fails. The returned pointer must be freed using ri_memory_cache_free().

§Initial State

A newly created memory cache:

  • Contains zero entries
  • Has no memory usage
  • Uses default LRU eviction
  • No maximum capacity enforcement until configured

§Usage Pattern

RiMemoryCache* cache = ri_memory_cache_new();
if (cache == NULL) {
    // Handle allocation failure
    return ERROR_MEMORY_ALLOCATION;
}

// Configure capacity if needed
ri_memory_cache_set_max_size(cache, 100000);

// Use cache operations
ri_memory_cache_set(cache, "key", "value", 5);
char* value = ri_memory_cache_get(cache, "key", NULL);

// Cleanup
ri_memory_cache_free(cache);

§Performance Considerations

For optimal performance:

  • Configure capacity before heavy usage
  • Batch similar operations together
  • Use appropriate serialization format
  • Monitor cache hit rate for tuning