1
0

ConfigCacheService.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use Config;
  5. use App\Models\ConfigCache as ConfigCacheModel;
  6. class ConfigCacheService
  7. {
  8. const CACHE_KEY = 'config_cache:_v0-key:';
  9. public static function get($key)
  10. {
  11. $cacheKey = self::CACHE_KEY . $key;
  12. $ttl = now()->addHours(12);
  13. if(!config('instance.enable_cc')) {
  14. return config($key);
  15. }
  16. return Cache::remember($cacheKey, $ttl, function() use($key) {
  17. $allowed = [
  18. 'app.name',
  19. 'app.short_description',
  20. 'app.description',
  21. 'app.rules',
  22. 'pixelfed.max_photo_size',
  23. 'pixelfed.max_album_length',
  24. 'pixelfed.image_quality',
  25. 'pixelfed.media_types',
  26. 'pixelfed.open_registration',
  27. 'federation.activitypub.enabled',
  28. 'instance.stories.enabled',
  29. 'pixelfed.oauth_enabled',
  30. 'pixelfed.import.instagram.enabled',
  31. 'pixelfed.bouncer.enabled',
  32. 'pixelfed.enforce_email_verification',
  33. 'pixelfed.max_account_size',
  34. 'pixelfed.enforce_account_limit',
  35. 'uikit.custom.css',
  36. 'uikit.custom.js',
  37. 'uikit.show_custom.css',
  38. 'uikit.show_custom.js',
  39. 'about.title',
  40. 'pixelfed.cloud_storage',
  41. 'account.autofollow',
  42. 'account.autofollow_usernames',
  43. 'config.discover.features',
  44. // 'system.user_mode'
  45. ];
  46. if(!config('instance.enable_cc')) {
  47. return config($key);
  48. }
  49. if(!in_array($key, $allowed)) {
  50. return config($key);
  51. }
  52. $v = config($key);
  53. $c = ConfigCacheModel::where('k', $key)->first();
  54. if($c) {
  55. return $c->v ?? config($key);
  56. }
  57. if(!$v) {
  58. return;
  59. }
  60. $cc = new ConfigCacheModel;
  61. $cc->k = $key;
  62. $cc->v = $v;
  63. $cc->save();
  64. return $v;
  65. });
  66. }
  67. public static function put($key, $val)
  68. {
  69. $exists = ConfigCacheModel::whereK($key)->first();
  70. if($exists) {
  71. $exists->v = $val;
  72. $exists->save();
  73. Cache::put(self::CACHE_KEY . $key, $val, now()->addHours(12));
  74. return self::get($key);
  75. }
  76. $cc = new ConfigCacheModel;
  77. $cc->k = $key;
  78. $cc->v = $val;
  79. $cc->save();
  80. Cache::put(self::CACHE_KEY . $key, $val, now()->addHours(12));
  81. return self::get($key);
  82. }
  83. }