Skip to content

Commit

Permalink
Allow JSON cache in Docker-mode
Browse files Browse the repository at this point in the history
(i.e. Docker does not require to use Redis)
  • Loading branch information
kimbtech committed Feb 26, 2024
1 parent a7c0227 commit ba1dcc6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The entire API is bundled in a [Docker Image](https://hub.docker.com/r/kimbtechn
*If hosted in a local network using `all` is recommended.*
- `CONF_STREAM_JSON` Url to a JSON list of streams or `false` to disable (see [Own Streams](#own-streams))
- There are some more options, see defaults in [docker-compose.yml](https://github.com/KIMB-technologies/Radio-API/blob/master/docker-compose.yml).
- The default setup uses Redis for fast caching of values. Redis may be disable by setting `CONF_USE_JSON_CACHE=true`, which enables an json file based caching as fallback (cached items are then stored in `./data/cache`).
- There are two ways to store which episodes of podcasts have already been listened to (new ones are marked by `*`)
- Create a cron job to `/cron.php`, e.g., `docker exec --user www-data radio_api php /cron.php`. (This will dump the already played episodes to a JSON file in `./data/` and *Radio-API* will load the file into redis on container startup).
- Use the data volume of Redis. (Redis will (re-)load its dump files on container startup.)
Expand Down Expand Up @@ -80,6 +81,7 @@ The image of [Radio DNS](https://hub.docker.com/r/kimbtechnologies/radio_dns) is
- `CONF_CACHE_DIR` (optional) Change the folder used by the file based cache (defaults to `./data/cache/`).
- `CONF_IM_EXPORT_TOKEN` (optional) Define a token for use with the Im- & Export web interface *Im- & Export* [↓](#im---export).
- **Attention:** Optional parameters have a leading `____` in the default `env.json`, make sure to remove them.
- The `CONF_REDIS_*` values are ignored and `CONF_USE_JSON_CACHE` is always `true`.
- Make sure, that *Radio-API* is available at port `80` for requests with the hostname `*.wifiradiofrontier.com` and `CONF_DOMAIN`.
- Block HTTP access to `./data/` (and `./classes/`) for security reasons (might be omitted in a local network installation).
- Rewrite requests to PHP:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
2.8.2
2.8.3
2.8
2
1 change: 1 addition & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
- CONF_ALLOWED_DOMAIN=all
- CONF_SHUFFLE_MUSIC=true
- CONF_CACHE_EXPIRE=1200
#- CONF_USE_JSON_CACHE=true
- CONF_REDIS_HOST=redis
- CONF_STREAM_JSON=false #http://192.168.0.10:8081/list.json
- CONF_IM_EXPORT_TOKEN=LP75Djdj195DL8SZnfY3
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ services:
- CONF_ALLOWED_DOMAIN=all # allowed ips for access, list of DynDNS domainnames (divided by ','), or 'all' to allow all ips
- CONF_SHUFFLE_MUSIC=true # random shuffle music in nextcloud radio stations
- CONF_CACHE_EXPIRE=1200 # cache expire time of ips, podcasts, ...
- CONF_USE_JSON_CACHE=false # set to 'true' to disable redis cache and use a simple file-based cache
- CONF_REDIS_HOST=redis # the redis hostname
# - CONF_REDIS_PORT=6379 # default 6379
# - CONF_REDIS_PASS= # default no auth
Expand All @@ -24,6 +25,8 @@ services:
# -CONF_IM_EXPORT_TOKEN= # define a token for use with the im-, export web interface at ./gui/im-export.php
depends_on:
- redis

# remove the following part, if 'CONF_USE_JSON_CACHE=true', i.e., redis is not used!
redis:
image: redis:alpine
container_name: radio_api_redis
Expand Down
2 changes: 1 addition & 1 deletion php/classes/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Cache implements CacheInterface {
private $s;

public function __construct(string $group){
$this->s = DOCKER_MODE ? new RedisCache($group) : new JSONCache($group);
$this->s = DOCKER_MODE && !Config::USE_JSON_CACHE ? new RedisCache($group) : new JSONCache($group);
}
public function getAllKeysOfGroup() : array {
return $this->s->getAllKeysOfGroup();
Expand Down
36 changes: 23 additions & 13 deletions php/classes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
!empty($ENV['CONF_IM_EXPORT_TOKEN']) && Helper::checkFilename($ENV['CONF_IM_EXPORT_TOKEN']) && strlen($ENV['CONF_IM_EXPORT_TOKEN']) > 15 ?
$ENV['CONF_IM_EXPORT_TOKEN'] : false
);
define(
'ENV_USE_JSON_CACHE',
!empty($_ENV['CONF_USE_JSON_CACHE']) && $_ENV['CONF_USE_JSON_CACHE'] == 'true'
);

// IP on reverse proxy setup
if( !empty($_SERVER['HTTP_X_REAL_IP']) ){
Expand All @@ -77,7 +81,7 @@ class Config {
/**
* The system's version.
*/
const VERSION = 'v2.8.2';
const VERSION = 'v2.8.3';

/**
* The real domain which should be used.
Expand Down Expand Up @@ -121,6 +125,11 @@ class Config {
*/
const IM_EXPORT_TOKEN = ENV_IM_EXPORT_TOKEN;

/**
* Always use json cache, even in Docker-Mode
*/
const USE_JSON_CACHE = ENV_USE_JSON_CACHE;

/**
* Store redis cache for ALLOWED_DOMAINS
*/
Expand Down Expand Up @@ -192,21 +201,22 @@ public static function checkAccess( ?string $mac = null ) : void {
}

/**
* Sets the redis server copnnection details using the env vars.
* Sets the redis server connection details using the env vars.
* Should be always called before creating a RedisCache.
*/
public static function setRedisServer() : void {
if(!DOCKER_MODE){ // Redis only in Docker mode
return;
}
if( isset( $_ENV['CONF_REDIS_HOST'], $_ENV['CONF_REDIS_PORT'], $_ENV['CONF_REDIS_PASS'] ) ){
RedisCache::setRedisServer($_ENV['CONF_REDIS_HOST'], $_ENV['CONF_REDIS_PORT'], $_ENV['CONF_REDIS_PASS']);
}
else if( isset( $_ENV['CONF_REDIS_HOST'], $_ENV['CONF_REDIS_PORT'] ) ){
RedisCache::setRedisServer($_ENV['CONF_REDIS_HOST'], $_ENV['CONF_REDIS_PORT']);
}
else if( isset( $_ENV['CONF_REDIS_HOST'] ) ){
RedisCache::setRedisServer($_ENV['CONF_REDIS_HOST']);
// Redis only in Docker mode and if not USE_JSON_CACHE
if(DOCKER_MODE && !self::USE_JSON_CACHE){
// configure redis
if( isset( $_ENV['CONF_REDIS_HOST'], $_ENV['CONF_REDIS_PORT'], $_ENV['CONF_REDIS_PASS'] ) ){
RedisCache::setRedisServer($_ENV['CONF_REDIS_HOST'], $_ENV['CONF_REDIS_PORT'], $_ENV['CONF_REDIS_PASS']);
}
else if( isset( $_ENV['CONF_REDIS_HOST'], $_ENV['CONF_REDIS_PORT'] ) ){
RedisCache::setRedisServer($_ENV['CONF_REDIS_HOST'], $_ENV['CONF_REDIS_PORT']);
}
else if( isset( $_ENV['CONF_REDIS_HOST'] ) ){
RedisCache::setRedisServer($_ENV['CONF_REDIS_HOST']);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions php/classes/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Output {
'Top Click' => ['Top Click', 'Am meisten gehört'],
'Top Vote' => ['Top Vote', 'Am höchsten bewertet'],
'Next Page' => ['Next Page', 'Nächste Seite'],
'You do not have last stations.' => ['You do not have last stations.', 'Sie haben keine zuletzt gehörten Sender!']
);

/**
Expand Down

0 comments on commit ba1dcc6

Please sign in to comment.