summaryrefslogtreecommitdiff
path: root/indra/llfilesystem/lldiskcache.h
blob: 1618cec6a6a72c9a6a0f060a3794faf357982d7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * @file lldiskcache.h
 * @brief The disk cache implementation.
 *
 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2020, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#ifndef _LLDISKCACHE
#define _LLDISKCACHE

class LLDiskCache
{
    public:
        LLDiskCache(const std::string cache_dir);
        ~LLDiskCache();

        /**
         * Update the "last write time" of a file to "now". This must be called whenever a 
         * file in the cache is read (not written) so that the last time the file was 
         * accessed which is used in the mechanism for purging the cache, is up to date.
         */
        void updateFileAccessTime(const std::string file_path);

        /**
         * Purge the oldest items in the cache so that the combined size of all files
         * is no bigger than mMaxSizeBytes.
         */
        void purge();

        /**
         * Clear the cache by removing the files in the cache directory individually
         */
        void clearCache(const std::string cache_dir);

        /**
         * Manage max size in bytes of cache - use a discrete setter/getter so the value can
         * be changed in the preferences and cache cleared/purged without restarting viewer
         * to instantiate this class again.
         */
        void setMaxSizeBytes(const uintmax_t size_bytes) { mMaxSizeBytes = size_bytes; }
        uintmax_t getMaxSizeBytes() const { return mMaxSizeBytes; }

    private:
        /**
         * Utility function to gather the total size the files in a given
         * directory. Primarily used here to determine the directory size 
         * before and after the cache purge
         */
        uintmax_t dirFileSize(const std::string dir);

    private:
        /**
         * Default of 20MB seems reasonable - it will likely be reset
         * immediately anyway using a value from the Viewer settings
         */
        const uintmax_t mDefaultSizeBytes = 20 * 1024 * 1024;
        uintmax_t mMaxSizeBytes;

        /**
         * The folder that holds the cached files. The consumer of this
         * class must avoid letting the user set this location as a malicious
         * setting could potentially point it at a non-cache directory (for example,
         * the Windows System dir) with disastrous results.
         */
        std::string mCacheDir;

        /**
         * This is set from the CacheDebugInfo global setting and
         * when enabled, displays additional debugging information in
         * various parts of the code
         */
        bool mCacheDebugInfo;
};

#endif // _LLDISKCACHE