Commit 2a1be493 authored by James Westman's avatar James Westman
Browse files

file-cache: Automatically purge the cache

Purge the cache the first time a tile is saved to the cache, and again
whenever the size limit is reached.
parent 790ffbc5
Pipeline #284680 passed with stage
in 2 minutes and 22 seconds
......@@ -352,6 +352,7 @@ shumate_file_cache_init (ShumateFileCache *file_cache)
priv->cache_dir = NULL;
priv->size_limit = 100000000;
priv->size_estimate = -1;
priv->cache_dir = NULL;
priv->db = NULL;
priv->stmt_select = NULL;
......@@ -692,7 +693,9 @@ purge_cache (GTask *task,
rc = sqlite3_step (stmt);
}
g_debug ("Cache size is now %d", current_size);
priv->size_estimate = current_size;
query = sqlite3_mprintf ("UPDATE tiles SET popularity = popularity - %d",
highest_popularity);
......@@ -1029,6 +1032,7 @@ on_file_written (GObject *object, GAsyncResult *res, gpointer user_data)
g_autoptr(sqlite_str) query = NULL;
g_autoptr(sqlite_str) sql_error = NULL;
GError *error = NULL;
guint tile_size = g_bytes_get_size (data->bytes);
g_output_stream_write_all_finish (G_OUTPUT_STREAM (object), res, NULL, &error);
if (error != NULL)
......@@ -1038,7 +1042,7 @@ on_file_written (GObject *object, GAsyncResult *res, gpointer user_data)
}
query = sqlite3_mprintf ("REPLACE INTO tiles (filename, etag, size) VALUES (%Q, %Q, %d)",
data->filename, data->etag, g_bytes_get_size (data->bytes));
data->filename, data->etag, tile_size);
sqlite3_exec (priv->db, query, NULL, NULL, &sql_error);
if (sql_error != NULL)
{
......@@ -1047,6 +1051,16 @@ on_file_written (GObject *object, GAsyncResult *res, gpointer user_data)
return;
}
priv->size_estimate += tile_size;
if (priv->size_estimate < tile_size || priv->size_estimate > priv->size_limit + 5000000)
{
/* automatically purge the cache if the size estimate is 5MB over
* the limit, or if the size estimate was its initial value of -1 */
shumate_file_cache_purge_cache_async (data->self, NULL, NULL, NULL);
}
g_task_return_boolean (task, TRUE);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment