Skip to content

Caching requests for game server search #39

@d3m37r4

Description

@d3m37r4

You can cache the results of a database query so that repeated requests to the same game server do not waste resources querying the database each time. For example, if the IP, port, and token remain unchanged, the search result can be stored in the cache.

$ip = $request->header('X-Server-IP');
$port = $request->header('X-Server-Port');
$token = $request->header('X-Server-Token');

$cacheKey = "server_{$ip}_{$port}";
$gameServer = Cache::remember($cacheKey, 600, function () use ($ip, $port) {
    return GameServer::where('ip', $ip)
        ->where('port', $port)
        ->first();
});

Cache::remember(): Use the remember method, which checks if there is an entry in the cache and if there is not, performs a database query and stores the result for 10 minutes (600 seconds).
cacheKey: Create a unique key for the cache based on the server IP and port. This prevents duplicate servers with the same IP and port.

But you must remember to update the data when you update the GameServer model (cache reset).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions