Avoid optional disk cache usage if it is owned by a different user. refs #2659.
authorTom Clegg <tom@curoverse.com>
Thu, 5 Feb 2015 21:22:34 +0000 (16:22 -0500)
committerTom Clegg <tom@curoverse.com>
Thu, 5 Feb 2015 21:57:04 +0000 (16:57 -0500)
services/api/lib/current_api_client.rb

index 197dcd8f3e2c3cdff5419566fd319128be71e25f..6c1ff2807e12705edb108b4f92147a195eeff710 100644 (file)
@@ -187,9 +187,24 @@ module CurrentApiClient
   # If the given value is nil, or the cache has been cleared since it
   # was set, yield. Otherwise, return the given value.
   def check_cache value
-    Rails.cache.fetch "CurrentApiClient.$globals" do
-      value = nil
-      true
+    if not Rails.env.test? and
+        ActionController::Base.cache_store.is_a? ActiveSupport::Cache::FileStore and
+        not File.owned? ActionController::Base.cache_store.cache_path
+      # If we don't own the cache dir, we're probably
+      # crunch-dispatch. Whoever we are, using this cache is likely to
+      # either fail or screw up the cache for someone else. So we'll
+      # just assume the $globals are OK to live forever.
+      #
+      # The reason for making the globals expire with the cache in the
+      # first place is to avoid leaking state between test cases: in
+      # production, we don't expect the database seeds to ever go away
+      # even when the cache is cleared, so there's no particular
+      # reason to expire our global variables.
+    else
+      Rails.cache.fetch "CurrentApiClient.$globals" do
+        value = nil
+        true
+      end
     end
     return value unless value.nil?
     yield