()
| 620 | } |
| 621 | |
| 622 | function purge() |
| 623 | { |
| 624 | if(!data) data = storage.get('cache') || {}; |
| 625 | |
| 626 | let time = app.time(); |
| 627 | |
| 628 | let cacheMaxSize = config.cacheMaxSize * 1000 * 1000; |
| 629 | let cacheMaxOld = config.cacheMaxOld * 60 * 60 * 24; |
| 630 | |
| 631 | let dataArray = []; |
| 632 | |
| 633 | // Remove not usage files |
| 634 | for(let sha in data) |
| 635 | { |
| 636 | if(time - data[sha].lastAccess > cacheMaxOld) |
| 637 | { |
| 638 | deleteInCacheSha(sha); |
| 639 | } |
| 640 | else |
| 641 | { |
| 642 | dataArray.push({ |
| 643 | sha: sha, |
| 644 | lastAccess: data[sha].lastAccess, |
| 645 | }); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | // Remove unreferenced files |
| 650 | let files = fs.readdirSync(cache.folder); |
| 651 | |
| 652 | for(let i = 0, len = files.length; i < len; i++) |
| 653 | { |
| 654 | let file = files[i]; |
| 655 | |
| 656 | let sha = extract(/^([a-z0-9]+)\.jpg/iu, file, 1); |
| 657 | |
| 658 | if(sha && !data[sha]) |
| 659 | deleteInCacheSha(sha); |
| 660 | } |
| 661 | |
| 662 | // Remove if exede cache max size |
| 663 | let cacheSize = fileManager.dirSizeSync(cache.folder); |
| 664 | |
| 665 | if(cacheSize > cacheMaxSize) |
| 666 | { |
| 667 | let cacheMaxSizeMargin = cacheMaxSize * 0.8; // Remove 20% if cache exceeds maximum size to avoid running this every time |
| 668 | |
| 669 | dataArray.sort(function(a, b) { |
| 670 | |
| 671 | if(a.lastAccess === b.lastAccess) |
| 672 | return 0; |
| 673 | |
| 674 | return a.lastAccess > b.lastAccess ? 1 : -1; |
| 675 | |
| 676 | }); |
| 677 | |
| 678 | for(let i = 0, len = dataArray.length; i < len; i++) |
| 679 | { |
nothing calls this directly
no test coverage detected