Skip to content

Commit 4392a9d

Browse files
committed
Revert "Feature/l1 memcached cache middleware (#412)"
This reverts commit d0c5913.
1 parent a7de94a commit 4392a9d

8 files changed

Lines changed: 370 additions & 520 deletions

File tree

.github/workflows/push.yml

Lines changed: 177 additions & 177 deletions
Large diffs are not rendered by default.

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ENV PATH=$NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
4848

4949
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
5050

51-
RUN install-php-extensions bcmath exif gettext gd imagick mbstring openssl pcntl pdo pdo_mysql sockets ${XDEBUG_VERSION} zip apcu redis igbinary memcached
51+
RUN install-php-extensions bcmath exif gettext gd imagick mbstring openssl pcntl pdo pdo_mysql sockets ${XDEBUG_VERSION} zip apcu redis igbinary
5252

5353
# XDEBUG
5454
COPY docker-compose/php/docker-php-ext-xdebug.ini $PHP_DIR/conf.d/docker-php-ext-xdebug.ini
@@ -69,4 +69,4 @@ RUN chmod 777 -R storage
6969

7070
# access to http://localhost:8002/apc.php to see APC statistics
7171

72-
RUN cd /var/www/public && curl -LO https://raw.githubusercontent.com/krakjoe/apcu/master/apc.php
72+
RUN cd /var/www/public && curl -LO https://raw.githubusercontent.com/krakjoe/apcu/master/apc.php

app/Http/Middleware/CacheMiddleware.php

Lines changed: 33 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php namespace App\Http\Middleware;
22

3-
use App\Utils\Cache\MemCache;
43
use Closure;
54
use Illuminate\Http\JsonResponse;
65
use Illuminate\Support\Facades\Cache;
@@ -88,82 +87,49 @@ public function handle($request, Closure $next, $cache_lifetime, $cache_region =
8887
}
8988
}
9089
$status = 200;
91-
$wasMemCacheHit = false;
9290
$wasHit = false;
93-
$data = null;
94-
9591
if ($regionTag) {
9692
Log::debug("CacheMiddleware: using region tag {$regionTag} ip {$ip} agent {$agent}");
97-
// try L1 APC
98-
$encoded = MemCache::get($key);
99-
$wasMemCacheHit = $encoded !== null;
100-
if($wasMemCacheHit){
101-
Log::debug("CacheMiddleware:: MemcCache Hit");
102-
}
103-
if(!$wasMemCacheHit) {
104-
// then L2 Redis
105-
$wasHit = Cache::tags($regionTag)->has($key);
106-
Log::debug($wasHit ? "CacheMiddleware: cache HIT Redis (tagged)" : "CacheMiddleware: cache MISS (tagged)", [
107-
'tag' => $regionTag,
108-
'ip' => $ip,
109-
'agent' => $agent,
110-
'key' => $key,
111-
]);
112-
113-
$encoded = Cache::tags($regionTag)
114-
->remember($key, $cache_lifetime, function () use ($next, $request, $regionTag, $key, $cache_lifetime, &$status, $ip, $agent) {
115-
$resp = $next($request);
116-
if ($resp instanceof JsonResponse) {
117-
$status = $resp->getStatusCode();
118-
if ($status === 200) {
119-
return $this->encode($resp->getData(true));
120-
}
121-
}
122-
// don’t cache non-200 or non-JSON
123-
return Cache::get($key);
124-
});
125-
126-
127-
// backfill APC only if we actually have a value
128-
if ($encoded !== null) { // avoid null writes
129-
MemCache::put($key, $encoded, $cache_lifetime, $regionTag);
130-
}
131-
}
132-
$data = $this->decode($encoded);
133-
} else {
134-
// try L1 APC
135-
$encoded = MemCache::get($key);
136-
$wasMemCacheHit = !is_null($encoded);
137-
if($wasMemCacheHit){
138-
Log::debug("CacheMiddleware:: MemcCache Hit");
139-
}
140-
if(!$wasMemCacheHit) {
141-
// then L2 Redis
142-
143-
$wasHit = Cache::has($key);
144-
145-
Log::debug($wasHit ? "CacheMiddleware: cache HIT" : "CacheMiddleware: cache MISS", [
146-
'ip' => $ip,
147-
'agent' => $agent,
148-
'key' => $key,
149-
]);
150-
151-
$encoded = Cache::remember($key, $cache_lifetime, function () use ($next, $request, $key, &$status, $ip, $agent) {
93+
$wasHit = Cache::tags($regionTag)->has($key);
94+
Log::debug($wasHit ? "CacheMiddleware: cache HIT (tagged)" : "CacheMiddleware: cache MISS (tagged)", [
95+
'tag' => $regionTag,
96+
'ip' => $ip,
97+
'agent' => $agent,
98+
'key' => $key,
99+
]);
100+
101+
$encoded = Cache::tags($regionTag)
102+
->remember($key, $cache_lifetime, function() use ($next, $request, $regionTag, $key, $cache_lifetime, &$status,$ip, $agent) {
152103
$resp = $next($request);
153104
if ($resp instanceof JsonResponse) {
154105
$status = $resp->getStatusCode();
155-
if ($status === 200)
106+
if($status === 200) {
156107
return $this->encode($resp->getData(true));
108+
}
157109
}
110+
// don’t cache non-200 or non-JSON
158111
return Cache::get($key);
159112
});
160-
// store at APC
161-
if ($encoded !== null) { // avoid null writes
162-
MemCache::put($key, $encoded, $cache_lifetime);
113+
$data = $this->decode($encoded);
114+
} else {
115+
$wasHit = Cache::has($key);
116+
117+
Log::debug($wasHit ? "CacheMiddleware: cache HIT" : "CacheMiddleware: cache MISS", [
118+
'ip' => $ip,
119+
'agent' => $agent,
120+
'key' => $key,
121+
]);
122+
123+
$encoded = Cache::remember($key, $cache_lifetime, function() use ($next, $request, $key, &$status, $ip, $agent) {
124+
$resp = $next($request);
125+
if ($resp instanceof JsonResponse) {
126+
$status = $resp->getStatusCode();
127+
if($status === 200)
128+
return $this->encode($resp->getData(true));
163129
}
164-
165-
$data = $this->decode($encoded);
166-
}
130+
return Cache::get($key);
131+
});
132+
$data = $this->decode($encoded);
167133
}
168134
// safe guard
169135
if ($data === null) $data = is_array($encoded) ? $encoded : [];
@@ -177,7 +143,7 @@ public function handle($request, Closure $next, $cache_lifetime, $cache_region =
177143
$response->headers->addCacheControlDirective('must-revalidate', true);
178144
$response->headers->addCacheControlDirective('proxy-revalidate', true);
179145
$response->headers->add([
180-
'X-Cache-Result' => $wasMemCacheHit ? 'HIT MemcCache' : ($wasHit ? 'HIT REDIS' : 'MISS'),
146+
'X-Cache-Result' => $wasHit ? 'HIT':'MISS',
181147
]);
182148
Log::debug( "CacheMiddleware: returning response", [
183149
'ip' => $ip,

app/Services/Model/Imp/ProcessScheduleEntityLifeCycleEventService.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ public function process(string $entity_operator, int $summit_id, int $entity_id,
151151
}
152152

153153
if (!empty($cache_region_key)) {
154-
Log::debug("ProcessScheduleEntityLifeCycleEventService::process", ['cache_region_key' => $cache_region_key]);
155154
$this->cache_service->clearCacheRegion($cache_region_key);
156155
}
157156

app/Services/Utils/RedisCacheService.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* limitations under the License.
1313
**/
1414

15-
use App\Utils\Cache\MemCache;
1615
use Illuminate\Support\Facades\Cache;
1716
use Illuminate\Support\Facades\Log;
1817
use libs\utils\ICacheService;
@@ -332,17 +331,15 @@ public function ttl($key)
332331
return (int)$conn->ttl($key);
333332
}, 0);
334333
}
335-
334+
336335
/**
337336
* @param string $cache_region_key
338337
* @return void
339338
*/
340339
public function clearCacheRegion(string $cache_region_key): void
341340
{
342-
Log::debug("RedisCacheService::clearCacheRegion", ["key" => $cache_region_key]);
343341
if (!empty($cache_region_key)) {
344342
Cache::tags($cache_region_key)->flush();
345-
MemCache::apcClearRegion($cache_region_key);
346343
if($this->exists($cache_region_key)){
347344
Log::debug(sprintf("RedisCacheService::clearCacheRegion will clear cache region %s", $cache_region_key));
348345
$region_data = $this->getSingleValue($cache_region_key);

app/Utils/Cache/MemCache.php

Lines changed: 0 additions & 75 deletions
This file was deleted.

config/cache.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,12 @@
4949

5050
'memcached' => [
5151
'driver' => 'memcached',
52-
//'persistent_id' => 'host-cache',
53-
'sasl' => [null, null],
54-
'servers' => [
55-
// UNIX socket (fastest)
52+
'servers' => [
5653
[
57-
'host' => env('MEMCACHED_SERVER_HOST', '/var/run/memcached/memcached.sock'),
58-
'port' => env('MEMCACHED_SERVER_PORT',0),
59-
'weight' => env('MEMCACHED_SERVER_WEIGHT',100)
54+
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
55+
'port' => env('MEMCACHED_PORT', 11211),
56+
'weight' => 100,
6057
],
61-
// or TCP if you prefer:
62-
// ['host' => '127.0.0.1', 'port' => 11211, 'weight' => 100],
6358
],
6459
],
6560

0 commit comments

Comments
 (0)