3 * Copyright (C) 2008 The Geeqie Team
5 * Author: Vladimir Nadvornik
7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
14 #include "filecache.h"
16 /* this implements a simple LRU algorithm */
18 struct _FileCacheData {
19 FileCacheReleaseFunc release;
26 FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
28 FileCacheData *fc = g_new(FileCacheData, 1);
29 fc->release = release;
31 fc->max_size = max_size;
36 gint file_cache_get(FileCacheData *fc, FileData *fd)
39 if ((work = g_list_find(fc->list, fd)))
41 fc->list = g_list_remove_link(fc->list, work);
42 fc->list = g_list_concat(work, fc->list);
43 DEBUG_1("cache hit: %s", fd->path);
46 DEBUG_1("cache miss: %s", fd->path);
50 void file_cache_put(FileCacheData *fc, FileData *fd, gulong size)
54 if ((work = g_list_find(fc->list, fd)))
56 /* entry already exists, move it to the beginning */
57 fc->list = g_list_remove_link(fc->list, work);
58 fc->list = g_list_concat(work, fc->list);
62 DEBUG_1("cache add: %s", fd->path);
64 fc->list = g_list_prepend(fc->list, fd);
65 fc->size++; /* FIXME: use size */
67 if (fc->size < fc->max_size) return;
70 work = g_list_last(fc->list);
72 fc->list = g_list_delete_link(fc->list, work);
73 DEBUG_1("cache remove: %s", last_fd->path);
75 file_data_unref(last_fd);