]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Store secondary storage hits in primary storage
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 18 Jul 2021 10:44:39 +0000 (12:44 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 19 Jul 2021 10:35:22 +0000 (12:35 +0200)
src/storage/Storage.cpp
test/suites/secondary_file.bash
test/suites/secondary_http.bash
test/suites/secondary_redis.bash

index 4f5203a569aeba2a437311e4942045f37902a142..90dd36eee091aa23ce622097f7d5615d1d2a8cd5 100644 (file)
@@ -186,6 +186,17 @@ Storage::get(const Digest& key, const core::CacheEntryType type)
   } catch (const Error& e) {
     throw Fatal("Error writing to {}: {}", tmp_file.path, e.what());
   }
+
+  m_primary_storage.put(key, type, [&](const std::string& path) {
+    try {
+      Util::copy_file(tmp_file.path, path);
+    } catch (const Error& e) {
+      LOG("Failed to copy {} to {}: {}", tmp_file.path, path, e.what());
+      // Don't indicate failure since get from primary storage was OK.
+    }
+    return true;
+  });
+
   return tmp_file.path;
 }
 
index 57a79b8e96e0d3f12a036da560cf24f56167868f..90cf8adc8e5009568c82137800fd79a27dc88a22 100644 (file)
@@ -1,6 +1,6 @@
 SUITE_secondary_file_SETUP() {
     unset CCACHE_NODIRECT
-    export CCACHE_SECONDARY_STORAGE="file://$PWD/secondary"
+    export CCACHE_SECONDARY_STORAGE="file:$PWD/secondary"
 
     generate_code 1 test.c
 }
@@ -29,8 +29,7 @@ SUITE_secondary_file() {
     $CCACHE_COMPILE -c test.c
     expect_stat 'cache hit (direct)' 2
     expect_stat 'cache miss' 1
-    expect_stat 'files in cache' 0
-    expect_stat 'files in cache' 0
+    expect_stat 'files in cache' 2 # fetched from secondary
     expect_file_count 3 '*' secondary # CACHEDIR.TAG + result + manifest
 
     # -------------------------------------------------------------------------
@@ -54,17 +53,20 @@ SUITE_secondary_file() {
     $CCACHE_COMPILE -c test.c
     expect_stat 'cache hit (direct)' 1
     expect_stat 'cache miss' 1
-    expect_stat 'files in cache' 0
+    expect_stat 'files in cache' 2 # fetched from secondary
     expect_file_count 3 '*' secondary # CACHEDIR.TAG + result + manifest
     expect_file_count 3 '*' secondary_2 # CACHEDIR.TAG + result + manifest
 
+    $CCACHE -C >/dev/null
+    expect_stat 'files in cache' 0
+
     rm -r secondary/??
     expect_file_count 1 '*' secondary # CACHEDIR.TAG
 
     $CCACHE_COMPILE -c test.c
     expect_stat 'cache hit (direct)' 2
     expect_stat 'cache miss' 1
-    expect_stat 'files in cache' 0
+    expect_stat 'files in cache' 2 # fetched from secondary_2
     expect_file_count 1 '*' secondary # CACHEDIR.TAG
     expect_file_count 3 '*' secondary_2 # CACHEDIR.TAG + result + manifest
 
@@ -86,14 +88,14 @@ SUITE_secondary_file() {
     $CCACHE_COMPILE -c test.c
     expect_stat 'cache hit (direct)' 1
     expect_stat 'cache miss' 1
-    expect_stat 'files in cache' 0
+    expect_stat 'files in cache' 2 # fetched from secondary
     expect_file_count 3 '*' secondary # CACHEDIR.TAG + result + manifest
 
     echo 'int x;' >> test.c
     $CCACHE_COMPILE -c test.c
     expect_stat 'cache hit (direct)' 1
     expect_stat 'cache miss' 2
-    expect_stat 'files in cache' 2
+    expect_stat 'files in cache' 4
     expect_file_count 3 '*' secondary # CACHEDIR.TAG + result + manifest
 
     # -------------------------------------------------------------------------
index b998748821b505c331f6b9ec760760bca98f98fc..f9974f41779c62dd4bd2b6d9c6c6878d9599bc60 100644 (file)
@@ -64,8 +64,7 @@ SUITE_secondary_http() {
     $CCACHE_COMPILE -c test.c
     expect_stat 'cache hit (direct)' 2
     expect_stat 'cache miss' 1
-    expect_stat 'files in cache' 0
-    expect_stat 'files in cache' 0
+    expect_stat 'files in cache' 2 # fetched from secondary
     expect_file_count 2 '*' secondary # result + manifest
 
     # -------------------------------------------------------------------------
@@ -134,8 +133,7 @@ SUITE_secondary_http() {
         $CCACHE_COMPILE -c test.c
         expect_stat 'cache hit (direct)' 2
         expect_stat 'cache miss' 1
-        expect_stat 'files in cache' 0
-        expect_stat 'files in cache' 0
+        expect_stat 'files in cache' 2 # fetched from secondary
         expect_file_count 2 '*' secondary # result + manifest
     fi
 }
index e2e107ee388be78b936c313fb7b29d73b42b10b6..1894412f48540098ca9bf06e01eb161f6f70936f 100644 (file)
@@ -76,7 +76,7 @@ SUITE_secondary_redis() {
     $CCACHE_COMPILE -c test.c
     expect_stat 'cache hit (direct)' 2
     expect_stat 'cache miss' 1
-    expect_stat 'files in cache' 0
+    expect_stat 'files in cache' 2 # fetched from secondary
     expect_number_of_redis_cache_entries 2 "$redis_url" # result + manifest
 
     # -------------------------------------------------------------------------
@@ -109,6 +109,6 @@ SUITE_secondary_redis() {
     $CCACHE_COMPILE -c test.c
     expect_stat 'cache hit (direct)' 2
     expect_stat 'cache miss' 1
-    expect_stat 'files in cache' 0
+    expect_stat 'files in cache' 2 # fetched from secondary
     expect_number_of_redis_cache_entries 2 "$redis_url" # result + manifest
 }