Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add size limit to cache #675

Merged
merged 3 commits into from
May 1, 2021

Conversation

Johannesd3
Copy link
Contributor

Limits cache size by calculating the size of the directory on loading of the application and keeping track of additions and removals. As soon as the limit is exceeded, the least recently accessed files are deleted.

Resolves #642.

core/src/cache.rs Outdated Show resolved Hide resolved
@Johannesd3
Copy link
Contributor Author

The command line argument is still missing, any suggestion how to parse it? Is there some standard notation for file sizes? I tried to google it, but I found nothing because I had no good keywords.

@kingosticks
Copy link
Contributor

Do you mean like "K" "M", "G" as used for human-readable output from ls? And like numfmt --from=iec gives you?

@Johannesd3
Copy link
Contributor Author

Exactly

@sashahilton00
Copy link
Member

From what I uderstand there is no agreed upon standard format that everyone uses. A bit of digging reveals there's JEDEC and ISO 80000 which several pieces of software use, then there's binary prefix (eg. 1Kb = 1024b, with other increments calculated based on 2^x, depending on the prefix), and then there's decimal prefix, which is simply powers of 10 (1Kb = 1000b. No one appears to agree on what to use, but most SaaS providers appear to go with decimal prefix, as it squares nicely wth the advertised capacity of hard drives (eg. 250Gb, 500Gb, etc.)

I've probably butchered the capitalisation on the prefixes above, luckily the software I have used thus far been fairly forgiving of correct vs incorrect capitalisation.

@kingosticks
Copy link
Contributor

There are two standards, SI (base 10) and IEC (base 2). The numfmt manual is a good reference with correct formatting etc. The auto options sounds reasonable to me:

numbers with ‘K’,‘M’,‘G’,‘T’,‘P’,‘E’,‘Z’,‘Y’ suffixes are interpreted as SI values, and numbers with ‘Ki’, ‘Mi’,‘Gi’,‘Ti’,‘Pi’,‘Ei’,‘Zi’,‘Yi’ suffixes are interpreted as IEC values.

@ashthespy
Copy link
Member

As usual relevant http:https://xkcd.com/394/
image

@Johannesd3
Copy link
Contributor Author

@Johannesd3
Copy link
Contributor Author

I just realized that caching doesn't work anymore as expected. Probably streaming mode was broken by #658, so you'll have to listen a track to its end before it is added to the cache. I'll try to find the error and create a PR asap.

@Johannesd3
Copy link
Contributor Author

Seems as if the check has hung up, dunno why. Could someone cancel it?

@ghost
Copy link

ghost commented Apr 19, 2021

I would like to point out that relying on access time is highly discouraged. For more info, read this stack answer for example.

Some operating systems, for example raspi os, add noatime to fstab by default.

Some users add noatime to fstab themselves to improve system performance.

Applications should not rely on access time and should either:

  • keep their own database of when a file was accessed, if this is required for the application
  • rely on other file properties

@Johannesd3
Copy link
Contributor Author

Johannesd3 commented Apr 19, 2021

If no access time is available, the modification time or creation time is used, or as last fallback the current time. This happens only on initialization, it keeps the access time in memory while the program is running.

librespot/core/src/cache.rs

Lines 107 to 116 in 2b36b65

// The first of the following timestamps which is available will be chosen as access time:
// 1. Access time
// 2. Modification time
// 3. Creation time
// 4. Current time
let access_time = metadata
.accessed()
.or_else(|_| metadata.modified())
.or_else(|_| metadata.created())
.unwrap_or_else(|_| SystemTime::now());

@sashahilton00
Copy link
Member

Looks good. Could you add the documentation to the wiki as well when you get a moment.

@sashahilton00 sashahilton00 merged commit 96dca28 into librespot-org:dev May 1, 2021
@Johannesd3 Johannesd3 deleted the limit-cache-size branch May 1, 2021 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants