pixelfed/app/Services/ConfigCacheService.php

103 lines
1.9 KiB
PHP
Raw Normal View History

2021-05-08 03:34:58 +00:00
<?php
namespace App\Services;
use Cache;
use Config;
use App\Models\ConfigCache as ConfigCacheModel;
class ConfigCacheService
{
2021-05-19 08:15:37 +00:00
const CACHE_KEY = 'config_cache:_v0-key:';
2021-05-08 03:34:58 +00:00
public static function get($key)
{
2021-05-19 08:15:37 +00:00
$cacheKey = self::CACHE_KEY . $key;
2021-05-08 03:34:58 +00:00
$ttl = now()->addHours(12);
2021-05-19 08:15:37 +00:00
2021-05-08 03:34:58 +00:00
return Cache::remember($cacheKey, $ttl, function() use($key) {
$allowed = [
'app.name',
'app.short_description',
'app.description',
'app.rules',
2021-05-12 04:07:55 +00:00
'pixelfed.max_photo_size',
'pixelfed.max_album_length',
'pixelfed.image_quality',
'pixelfed.media_types',
'pixelfed.open_registration',
'federation.activitypub.enabled',
'instance.stories.enabled',
2021-05-19 08:15:37 +00:00
'pixelfed.oauth_enabled',
2021-05-12 04:07:55 +00:00
'pixelfed.import.instagram.enabled',
'pixelfed.bouncer.enabled',
'pixelfed.enforce_email_verification',
'pixelfed.max_account_size',
'pixelfed.enforce_account_limit',
'uikit.custom.css',
'uikit.custom.js',
'uikit.show_custom.css',
2021-05-19 08:15:37 +00:00
'uikit.show_custom.js',
'about.title',
2021-06-11 03:07:35 +00:00
'pixelfed.cloud_storage',
'account.autofollow',
'account.autofollow_usernames'
2021-05-08 03:34:58 +00:00
];
if(!config('instance.enable_cc')) {
return config($key);
}
2021-05-08 03:34:58 +00:00
if(!in_array($key, $allowed)) {
return config($key);
}
$v = config($key);
$c = ConfigCacheModel::where('k', $key)->first();
if($c) {
2021-05-12 04:07:55 +00:00
return $c->v ?? config($key);
2021-05-08 03:34:58 +00:00
}
if(!$v) {
return;
}
$cc = new ConfigCacheModel;
$cc->k = $key;
$cc->v = $v;
$cc->save();
return $v;
});
}
public static function put($key, $val)
{
$exists = ConfigCacheModel::whereK($key)->first();
if($exists) {
$exists->v = $val;
$exists->save();
2021-05-19 08:15:37 +00:00
Cache::put(self::CACHE_KEY . $key, $val, now()->addHours(12));
2021-05-08 03:34:58 +00:00
return self::get($key);
}
$cc = new ConfigCacheModel;
$cc->k = $key;
$cc->v = $val;
$cc->save();
2021-05-19 08:15:37 +00:00
Cache::put(self::CACHE_KEY . $key, $val, now()->addHours(12));
2021-05-12 04:07:55 +00:00
2021-05-08 03:34:58 +00:00
return self::get($key);
}
}