Repository: Spotifyd/spotifyd (language: rust)

Issue:
limit cache size config
Thanks for spotifyd. I have to clear my cache manually every two weeks, because the cache on my raspberry2 is full and the daemon — and of coarse other processes as well — stucks and doesn't play any songs.
Limit  the maximum cache size possibility in config file would be nice.
Delete the oldest when max size is reached would be the smartest way, if it is easy to implement. But clear cache completely is also a valid solution for me and the easiest way....
THX


Discussion / hints:
To fix your specific problem, I guess you could disable caching completely (by omitting the `cache_directory` from your config file). If you don't need any of the features that spotifyd provides on top of `librespot` you could also [use the upstream project directly](https://github.com/librespot-org/librespot#quick-start) and use their `--cache-size-limit` cli option.

---

For anyone looking to implement this: `librespot` (the upstream dependency that spotifyd uses) allows to specify a max cache size and spotifyd could make use of that in this line:

https://github.com/Spotifyd/spotifyd/blob/f8fce08b4f81397d5628f40d9379e93ff7bbe073/src/config.rs#L591

which would need to be replaced by a proper value instead of `None`.

Fix patch:
diff --git a/docs/src/config/File.md b/docs/src/config/File.md
index 523b3140..ef873053 100644
--- a/docs/src/config/File.md
+++ b/docs/src/config/File.md
@@ -72,6 +72,10 @@ bitrate = 160
 # shell placeholders like $HOME or ~ don't work!
 cache_path = "cache_directory"
 
+# The maximal size of the cache directory in bytes
+# The example value corresponds to ~ 1GB
+max_cache_size = 1000000000
+
 # If set to true, audio data does NOT get cached.
 no_audio_cache = true
 
diff --git a/src/config.rs b/src/config.rs
index efbf02de..ab59b185 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -312,6 +312,10 @@ pub struct SharedConfigValues {
     #[structopt(long, parse(from_os_str), short, value_name = "string")]
     cache_path: Option<PathBuf>,
 
+    /// The maximal cache size in bytes
+    #[structopt(long)]
+    max_cache_size: Option<u64>,
+
     /// Disable the use of audio cache
     #[structopt(long)]
     #[serde(default)]
@@ -459,6 +463,7 @@ impl fmt::Debug for SharedConfigValues {
             .field("proxy", &self.proxy)
             .field("device_type", &self.device_type)
             .field("autoplay", &self.autoplay)
+            .field("max_cache_size", &self.max_cache_size)
             .finish()
     }
 }
@@ -521,7 +526,8 @@ impl SharedConfigValues {
             zeroconf_port,
             proxy,
             device_type,
-            use_mpris
+            use_mpris,
+            max_cache_size
         );
 
         // Handles boolean merging.
@@ -580,6 +586,7 @@ pub(crate) struct SpotifydConfig {
 pub(crate) fn get_internal_config(config: CliConfig) -> SpotifydConfig {
     let audio_cache = !config.shared_config.no_audio_cache;
 
+    let size_limit = config.shared_config.max_cache_size;
     let cache = config
         .shared_config
         .cache_path
@@ -590,7 +597,7 @@ pub(crate) fn get_internal_config(config: CliConfig) -> SpotifydConfig {
             Cache::new(
                 Some(path.clone()),
                 if audio_cache { Some(path) } else { None },
-                None,
+                size_limit,
             )
             .ok()
         });


Test patch: