We use cookies on our site to track certain metrics. Read our privacy policy here to see how we keep your personal data confidential and secure.

Handle-with-cache.c Direct

In systems programming, efficiency is paramount. Repeatedly opening, reading, or computing the same resource (a file, a network socket, a database row, or a complex calculation result) is wasteful. This is where caching becomes indispensable.

// Improved get_handle() with double-check UserProfile* get_user_profile_handle_safe(int user_id) { pthread_mutex_lock(&cache_lock); CacheEntry *entry = g_hash_table_lookup(handle_cache, &user_id); if (entry) { entry->ref_count++; pthread_mutex_unlock(&cache_lock); return entry->profile; } pthread_mutex_unlock(&cache_lock); // Load outside lock UserProfile *profile = load_user_profile_from_disk(user_id);

pthread_mutex_lock(&cache_lock); // Double-check: another thread might have inserted it while we were loading entry = g_hash_table_lookup(handle_cache, &user_id); if (entry) { // Discard our loaded profile and use the cached one free_user_profile(profile); entry->ref_count++; pthread_mutex_unlock(&cache_lock); return entry->profile; }

void release_user_profile_handle(UserProfile *profile) { if (!profile) return; handle-with-cache.c

// Store in cache (use user_id as key) int *key = malloc(sizeof(int)); *key = user_id; g_hash_table_insert(handle_cache, key, new_entry);

UserProfile* get_user_profile_handle(int user_id) { pthread_mutex_lock(&cache_lock); // Check cache CacheEntry *entry = g_hash_table_lookup(handle_cache, &user_id); if (entry) { // Cache hit entry->ref_count++; entry->last_access = time(NULL); pthread_mutex_unlock(&cache_lock); printf("Cache hit for user %d\n", user_id); return entry->profile; }

A common optimization is or using a per-key mutex: In systems programming, efficiency is paramount

The module handle-with-cache.c exemplifies a classic design pattern: the . A "handle" is an opaque pointer or identifier to a resource, and the cache stores recently accessed handles to avoid redundant initialization or I/O operations.

// Cache entry wrapper typedef struct { UserProfile *profile; time_t last_access; unsigned int ref_count; // Reference counting for safety } CacheEntry;

pthread_mutex_unlock(&cache_lock); } A cache without eviction is a memory leak. handle-with-cache.c should implement a policy like LRU (Least Recently Used) or TTL (Time To Live) . handle-with-cache

pthread_mutex_unlock(&cache_lock); return profile; }

// Find the entry for this profile (simplified; real code needs reverse mapping) GHashTableIter iter; gpointer key, value; g_hash_table_iter_init(&iter, handle_cache); while (g_hash_table_iter_next(&iter, &key, &value)) { CacheEntry *entry = value; if (entry->profile == profile) { entry->ref_count--; if (entry->ref_count == 0) { // Last reference - we could evict immediately or mark as stale printf("No more references to user %d, marking for eviction\n", *(int*)key); } break; } }

array(4) { ["state_code"]=> string(3) "MUS" ["city_code"]=> string(14) "OMN_AL_GHUBRAH" ["state_name"]=> string(17) "Muscat City Limit" ["city_name"]=> string(10) "Al Ghubrah" }
Added to cart
Cart Total OMR
Proceed to Checkout

People Also Bought

Loading...