ConfigCacheService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. 'instance.has_legal_notice',
  45. 'instance.avatar.local_to_cloud',
  46. 'pixelfed.directory',
  47. 'app.banner_image',
  48. 'pixelfed.directory.submission-key',
  49. 'pixelfed.directory.submission-ts',
  50. 'pixelfed.directory.has_submitted',
  51. 'pixelfed.directory.latest_response',
  52. 'pixelfed.directory.is_synced',
  53. 'pixelfed.directory.testimonials',
  54. 'instance.landing.show_directory',
  55. 'instance.landing.show_explore',
  56. 'instance.admin.pid',
  57. 'instance.banner.blurhash',
  58. 'autospam.nlp.enabled',
  59. // 'system.user_mode'
  60. ];
  61. if(!config('instance.enable_cc')) {
  62. return config($key);
  63. }
  64. if(!in_array($key, $allowed)) {
  65. return config($key);
  66. }
  67. $v = config($key);
  68. $c = ConfigCacheModel::where('k', $key)->first();
  69. if($c) {
  70. return $c->v ?? config($key);
  71. }
  72. if(!$v) {
  73. return;
  74. }
  75. $cc = new ConfigCacheModel;
  76. $cc->k = $key;
  77. $cc->v = $v;
  78. $cc->save();
  79. return $v;
  80. });
  81. }
  82. public static function put($key, $val)
  83. {
  84. $exists = ConfigCacheModel::whereK($key)->first();
  85. if($exists) {
  86. $exists->v = $val;
  87. $exists->save();
  88. Cache::put(self::CACHE_KEY . $key, $val, now()->addHours(12));
  89. return self::get($key);
  90. }
  91. $cc = new ConfigCacheModel;
  92. $cc->k = $key;
  93. $cc->v = $val;
  94. $cc->save();
  95. Cache::put(self::CACHE_KEY . $key, $val, now()->addHours(12));
  96. return self::get($key);
  97. }
  98. }