ConfigCacheService.php 2.0 KB

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