Skip to content

Commit

Permalink
Make it possible to prioritize items in blobcache
Browse files Browse the repository at this point in the history
Right now only used to make sure the TMDB configuration (which is small)
get stored without risking to be expired. If it is expired none of the
other images can be loaded
  • Loading branch information
andoma committed Feb 1, 2014
1 parent f5aaac5 commit 65ae428
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/api/tmdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ tmdb_configure(void)
"api_key", TMDB_APIKEY,
"language", getlang(),
NULL, NULL},
FA_COMPRESSION);
FA_COMPRESSION | FA_IMPORTANT);

if(result == NULL) {
TRACE(TRACE_INFO, "TMDB", "Unable to get configuration -- %s", errbuf);
Expand Down
5 changes: 4 additions & 1 deletion src/blobcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ int blobcache_get_meta(const char *key, const char *stash,
char **etag, time_t *mtime);

int blobcache_put(const char *key, const char *stash, buf_t *buf,
int maxage, const char *etag, time_t mtime);
int maxage, const char *etag, time_t mtime,
int flags);

#define BLOBCACHE_IMPORTANT_ITEM 0x1

void blobcache_init(void);

Expand Down
99 changes: 78 additions & 21 deletions src/blobcache_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
#include "settings.h"
#include "notifications.h"

#define BC2_MAGIC_current 0x62630206
// Flags

#define BC2_MAGIC_07 0x62630207
#define BC2_MAGIC_06 0x62630206
#define BC2_MAGIC_05 0x62630205

typedef struct blobcache_item {
Expand All @@ -56,19 +59,33 @@ typedef struct blobcache_item {
uint32_t bi_modtime;
uint32_t bi_size;
uint8_t bi_content_type_len;
uint8_t bi_flags;
} blobcache_item_t;

typedef struct blobcache_diskitem {
typedef struct blobcache_diskitem_06 {
uint64_t di_key_hash;
uint64_t di_content_hash;
uint32_t di_lastaccess;
uint32_t di_expiry;
uint32_t di_modtime;
uint32_t di_size;
uint8_t di_etaglen;
uint8_t di_content_type_len;
uint8_t di_etag[0];
} __attribute__((packed)) blobcache_diskitem_06_t;

typedef struct blobcache_diskitem_07 {
uint64_t di_key_hash;
uint64_t di_content_hash;
uint32_t di_lastaccess;
uint32_t di_expiry;
uint32_t di_modtime;
uint32_t di_size;
uint8_t di_flags;
uint8_t di_etaglen;
uint8_t di_content_type_len;
uint8_t di_etag[0];
} __attribute__((packed)) blobcache_diskitem_t;
} __attribute__((packed)) blobcache_diskitem_07_t;


TAILQ_HEAD(blobcache_flush_queue, blobcache_flush);
Expand Down Expand Up @@ -175,7 +192,7 @@ save_index(void)
uint8_t *out, *base;
int i;
blobcache_item_t *p;
blobcache_diskitem_t *di;
blobcache_diskitem_07_t *di;
size_t siz;

if(!index_dirty)
Expand All @@ -192,7 +209,7 @@ save_index(void)

for(i = 0; i < ITEM_HASH_SIZE; i++) {
for(p = hashvector[i]; p != NULL; p = p->bi_link) {
siz += sizeof(blobcache_diskitem_t);
siz += sizeof(blobcache_diskitem_07_t);
siz += p->bi_etag ? strlen(p->bi_etag) : 0;
items++;
}
Expand All @@ -203,7 +220,7 @@ save_index(void)
close(fd);
return;
}
*(uint32_t *)out = BC2_MAGIC_current;
*(uint32_t *)out = BC2_MAGIC_07;
out += 4;
*(uint32_t *)out = items;
out += 4;
Expand All @@ -212,16 +229,17 @@ save_index(void)
for(i = 0; i < ITEM_HASH_SIZE; i++) {
for(p = hashvector[i]; p != NULL; p = p->bi_link) {
const int etaglen = p->bi_etag ? strlen(p->bi_etag) : 0;
di = (blobcache_diskitem_t *)out;
di = (blobcache_diskitem_07_t *)out;
di->di_key_hash = p->bi_key_hash;
di->di_content_hash = p->bi_content_hash;
di->di_lastaccess = p->bi_lastaccess;
di->di_expiry = p->bi_expiry;
di->di_modtime = p->bi_modtime;
di->di_size = p->bi_size;
di->di_flags = p->bi_flags;
di->di_etaglen = etaglen;
di->di_content_type_len = p->bi_content_type_len;
out += sizeof(blobcache_diskitem_t);
out += sizeof(blobcache_diskitem_07_t);
if(etaglen) {
memcpy(out, p->bi_etag, etaglen);
out += etaglen;
Expand Down Expand Up @@ -260,7 +278,6 @@ load_index(void)
void *base;
int i;
blobcache_item_t *p;
blobcache_diskitem_t *di;
struct stat st;
uint8_t digest[20];

Expand Down Expand Up @@ -305,8 +322,13 @@ load_index(void)
int items = *(uint32_t *)in;
in += 4;

TRACE(TRACE_DEBUG, "blobcache", "Cache magic 0x%08x %d items", magic, items);

switch(magic) {
case BC2_MAGIC_current: {
case BC2_MAGIC_06:
TRACE(TRACE_INFO, "blobcache", "Upgrading from older format 0x%08x", magic);
// FALLTHRU
case BC2_MAGIC_07: {
if(*(uint32_t *)in > time(NULL)) {
TRACE(TRACE_INFO, "blobcache",
"Clock going backwards, throwing away cache");
Expand All @@ -329,19 +351,44 @@ load_index(void)


for(i = 0; i < items; i++) {
di = (blobcache_diskitem_t *)in;
p = pool_get(item_pool);
int etaglen;

switch(magic) {
case BC2_MAGIC_05:
case BC2_MAGIC_06: {
const blobcache_diskitem_06_t *di = (blobcache_diskitem_06_t *)in;

p->bi_key_hash = di->di_key_hash;
p->bi_content_hash = di->di_content_hash;
p->bi_lastaccess = di->di_lastaccess;
p->bi_expiry = di->di_expiry;
p->bi_modtime = di->di_modtime;
p->bi_size = di->di_size;
p->bi_content_type_len = di->di_content_type_len;
p->bi_flags = 0;
etaglen = di->di_etaglen;
in += sizeof(blobcache_diskitem_06_t);
}
break;

p->bi_key_hash = di->di_key_hash;
p->bi_content_hash = di->di_content_hash;
p->bi_lastaccess = di->di_lastaccess;
p->bi_expiry = di->di_expiry;
p->bi_modtime = di->di_modtime;
p->bi_size = di->di_size;
p->bi_content_type_len = di->di_content_type_len;
int etaglen = di->di_etaglen;
case BC2_MAGIC_07: {
const blobcache_diskitem_07_t *di = (blobcache_diskitem_07_t *)in;

p->bi_key_hash = di->di_key_hash;
p->bi_content_hash = di->di_content_hash;
p->bi_lastaccess = di->di_lastaccess;
p->bi_expiry = di->di_expiry;
p->bi_modtime = di->di_modtime;
p->bi_size = di->di_size;
p->bi_content_type_len = di->di_content_type_len;
p->bi_flags = di->di_flags;
etaglen = di->di_etaglen;
in += sizeof(blobcache_diskitem_07_t);
}
break;
}

in += sizeof(blobcache_diskitem_t);
if(etaglen) {
p->bi_etag = malloc(etaglen+1);
memcpy(p->bi_etag, in, etaglen);
Expand All @@ -364,7 +411,8 @@ load_index(void)
*/
int
blobcache_put(const char *key, const char *stash, buf_t *b,
int maxage, const char *etag, time_t mtime)
int maxage, const char *etag, time_t mtime,
int flags)
{
uint64_t dk = digest_key(key, stash);
uint64_t dc = digest_content(b->b_ptr, b->b_size);
Expand All @@ -391,6 +439,7 @@ blobcache_put(const char *key, const char *stash, buf_t *b,
p->bi_modtime = mtime;
p->bi_expiry = now + maxage;
p->bi_lastaccess = now;
p->bi_flags = flags;
mystrset(&p->bi_etag, etag);
hts_mutex_unlock(&cache_lock);
return 1;
Expand Down Expand Up @@ -424,6 +473,7 @@ blobcache_put(const char *key, const char *stash, buf_t *b,
current_cache_size += p->bi_size;
p->bi_content_type_len = b->b_content_type ?
strlen(rstr_get(b->b_content_type)) : 0;
p->bi_flags = flags;
hts_mutex_unlock(&cache_lock);
return 0;
}
Expand Down Expand Up @@ -662,6 +712,13 @@ accesstimecmp(const void *A, const void *B)
{
const blobcache_item_t *a = *(const blobcache_item_t **)A;
const blobcache_item_t *b = *(const blobcache_item_t **)B;

const int a_imp = !!(a->bi_flags & BLOBCACHE_IMPORTANT_ITEM);
const int b_imp = !!(b->bi_flags & BLOBCACHE_IMPORTANT_ITEM);

if(a_imp != b_imp)
return a_imp - b_imp;

return a->bi_lastaccess - b->bi_lastaccess;
}

Expand Down
2 changes: 1 addition & 1 deletion src/fileaccess/fa_imageloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ write_thumb(const AVCodecContext *src, const AVFrame *sframe,
int r = avcodec_encode_video2(ctx, &out, oframe, &got_packet);
if(r >= 0 && got_packet) {
buf_t *b = buf_create_and_adopt(out.size, out.data, &av_free);
blobcache_put(cacheid, "videothumb", b, INT32_MAX, NULL, mtime);
blobcache_put(cacheid, "videothumb", b, INT32_MAX, NULL, mtime, 0);
buf_release(b);
} else {
assert(out.data == NULL);
Expand Down
8 changes: 7 additions & 1 deletion src/fileaccess/fileaccess.c
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,13 @@ fa_load(const char *url, const char **vpaths,

if(data2 && cache_control != DISABLE_CACHE &&
(cache_control || max_age || etag || mtime)) {
no_change = blobcache_put(url, "fa_load", data2, max_age, etag, mtime);

int bc_flags = 0;
if(flags & FA_IMPORTANT)
bc_flags |= BLOBCACHE_IMPORTANT_ITEM;

no_change = blobcache_put(url, "fa_load", data2, max_age, etag, mtime,
bc_flags);

} else {
no_change = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/fileaccess/fileaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ LIST_HEAD(fa_protocol_list, fa_protocol);
#define FA_APPEND 0x800 /* Only if FA_WRITE:
Seek to EOF when opening
otherwise truncate */
#define FA_IMPORTANT 0x1000


/**
*
Expand Down
2 changes: 1 addition & 1 deletion src/js/js_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ js_cache_put(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)

// put json encoded object onto cache
snprintf(stash, sizeof(stash), "plugin/%s/%s", jsp->jsp_id, lstash);
blobcache_put(key, stash, b, maxage, NULL, 0);
blobcache_put(key, stash, b, maxage, NULL, 0, 0);
buf_release(b);
return JS_TRUE;
}
Expand Down

0 comments on commit 65ae428

Please sign in to comment.