Skip to content

Feature/l1 memcached cache middleware#412

Merged
smarcet merged 3 commits intomainfrom
feature/l1-memcached-cache-middleware
Oct 15, 2025
Merged

Feature/l1 memcached cache middleware#412
smarcet merged 3 commits intomainfrom
feature/l1-memcached-cache-middleware

Conversation

@smarcet
Copy link
Collaborator

@smarcet smarcet commented Oct 15, 2025

No description provided.

@smarcet smarcet requested review from Copilot and romanetar October 15, 2025 00:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a memcached-based L1 caching layer to improve API performance by adding an in-memory cache tier above the existing Redis cache system.

Key changes:

  • Adds memcached service to Docker Compose setup with appropriate configuration
  • Implements a two-tier caching strategy (L1 memcached, L2 Redis) in the cache middleware
  • Creates a dedicated MemCache utility class for memcached operations

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
docker-compose.yml Adds memcached service configuration and dependency
config/cache.php Updates memcached configuration to use UNIX socket for performance
app/Utils/Cache/MemCache.php New utility class providing memcached operations with region tracking
app/Http/Middleware/CacheMiddleware.php Implements L1/L2 caching strategy with memcached as primary tier
app/Services/Utils/RedisCacheService.php Integrates memcached clearing with Redis cache region clearing
app/Services/Model/Imp/ProcessScheduleEntityLifeCycleEventService.php Adds debug logging for cache operations
Dockerfile Adds memcached PHP extension
.github/workflows/push.yml Adds memcached service and configuration for CI
.env.example Adds memcached environment variables

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
'host' => env('MEMCACHED_SERVER_HOST', '/var/run/memcached/memcached.sock'),
'port' => env('MEMCACHED_SERVER_PORT',0),
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after comma in function call. Should be env('MEMCACHED_SERVER_PORT', 0) for consistency with Laravel coding standards.

Suggested change
'port' => env('MEMCACHED_SERVER_PORT',0),
'port' => env('MEMCACHED_SERVER_PORT', 0),

Copilot uses AI. Check for mistakes.
'weight' => 100,
'host' => env('MEMCACHED_SERVER_HOST', '/var/run/memcached/memcached.sock'),
'port' => env('MEMCACHED_SERVER_PORT',0),
'weight' => env('MEMCACHED_SERVER_WEIGHT',100)
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after comma in function call. Should be env('MEMCACHED_SERVER_WEIGHT', 100) for consistency with Laravel coding standards.

Suggested change
'weight' => env('MEMCACHED_SERVER_WEIGHT',100)
'weight' => env('MEMCACHED_SERVER_WEIGHT', 100)

Copilot uses AI. Check for mistakes.

$encoded = Cache::tags($regionTag)
->remember($key, $cache_lifetime, function() use ($next, $request, $regionTag, $key, $cache_lifetime, &$status,$ip, $agent) {
// try L1 APC
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment incorrectly refers to 'APC' but the code is using memcached. Should be 'try L1 memcached'.

Suggested change
// try L1 APC
// try L1 memcached

Copilot uses AI. Check for mistakes.
$encoded = MemCache::get($key);
$wasMemCacheHit = $encoded !== null;
if($wasMemCacheHit){
Log::debug("CacheMiddleware:: MemcCache Hit");
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in log message - 'MemcCache' should be 'Memcached'.

Suggested change
Log::debug("CacheMiddleware:: MemcCache Hit");
Log::debug("CacheMiddleware:: Memcached Hit");

Copilot uses AI. Check for mistakes.
});


// backfill APC only if we actually have a value
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment incorrectly refers to 'APC' but the code is using memcached. Should be 'backfill memcached only if we actually have a value'.

Suggested change
// backfill APC only if we actually have a value
// backfill memcached only if we actually have a value

Copilot uses AI. Check for mistakes.
}
$data = $this->decode($encoded);
} else {
// try L1 APC
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment incorrectly refers to 'APC' but the code is using memcached. Should be 'try L1 memcached'.

Copilot uses AI. Check for mistakes.
$encoded = MemCache::get($key);
$wasMemCacheHit = !is_null($encoded);
if($wasMemCacheHit){
Log::debug("CacheMiddleware:: MemcCache Hit");
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in log message - 'MemcCache' should be 'Memcached'.

Suggested change
Log::debug("CacheMiddleware:: MemcCache Hit");
Log::debug("CacheMiddleware:: Memcached Hit");

Copilot uses AI. Check for mistakes.
$status = $resp->getStatusCode();
if($status === 200)
return $this->encode($resp->getData(true));
// store at APC
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment incorrectly refers to 'APC' but the code is using memcached. Should be 'store at memcached'.

Suggested change
// store at APC
// store at memcached

Copilot uses AI. Check for mistakes.
$response->headers->addCacheControlDirective('proxy-revalidate', true);
$response->headers->add([
'X-Cache-Result' => $wasHit ? 'HIT':'MISS',
'X-Cache-Result' => $wasMemCacheHit ? 'HIT MemcCache' : ($wasHit ? 'HIT REDIS' : 'MISS'),
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in header value - 'MemcCache' should be 'Memcached'.

Suggested change
'X-Cache-Result' => $wasMemCacheHit ? 'HIT MemcCache' : ($wasHit ? 'HIT REDIS' : 'MISS'),
'X-Cache-Result' => $wasMemCacheHit ? 'HIT Memcached' : ($wasHit ? 'HIT REDIS' : 'MISS'),

Copilot uses AI. Check for mistakes.
…entService.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@smarcet smarcet force-pushed the feature/l1-memcached-cache-middleware branch from 5855552 to 99cd13d Compare October 15, 2025 20:24
@smarcet smarcet merged commit d0c5913 into main Oct 15, 2025
2 checks passed
smarcet added a commit that referenced this pull request Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants