Skip to content

Commit

Permalink
Allow specifying cache prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed Feb 23, 2024
1 parent 4a7e53b commit 31bc54d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
11 changes: 11 additions & 0 deletions config/geoip.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@

'cache_expires' => 30,

/*
|--------------------------------------------------------------------------
| Cache Prefix
|--------------------------------------------------------------------------
|
| Prefix used for cache keys (in addition to globally configured prefix).
|
*/

'cache_prefix' => 'geoip:',

/*
|--------------------------------------------------------------------------
| Default Location
Expand Down
14 changes: 12 additions & 2 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class Cache
*/
protected $expires;

/** Cache prefix */
protected string $prefix = '';

/**
* Create a new cache instance.
*
Expand All @@ -31,6 +34,13 @@ public function __construct(CacheManager $cache, $tags, $expires = 30)
{
$this->cache = $tags ? $cache->tags($tags) : $cache;
$this->expires = $expires;

}

/** @internal */
public function setPrefix(?string $prefix = null)

Check failure on line 41 in src/Cache.php

View workflow job for this annotation

GitHub Actions / Psalm

MissingReturnType

src/Cache.php:41:21: MissingReturnType: Method InteractionDesignFoundation\GeoIP\Cache::setPrefix does not have a return type, expecting void (see https://psalm.dev/050)
{
$this->prefix = (string) $prefix;
}

/**
Expand All @@ -42,7 +52,7 @@ public function __construct(CacheManager $cache, $tags, $expires = 30)
*/
public function get($name)
{
$value = $this->cache->get($name);
$value = $this->cache->get($this->prefix.$name);

return is_array($value)
? new Location($value)
Expand All @@ -59,7 +69,7 @@ public function get($name)
*/
public function set($name, Location $location)
{
return $this->cache->put($name, $location->toArray(), $this->expires);
return $this->cache->put($this->prefix.$name, $location->toArray(), $this->expires);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/GeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function __construct(array $config, CacheManager $cache)
$this->config('cache_tags'),
$this->config('cache_expires', 30)
);
$this->cache->setPrefix((string) $this->config('cache_prefix'));

// Set custom default location
$this->default_location = array_merge(
Expand Down

0 comments on commit 31bc54d

Please sign in to comment.