Permalink
Browse files

Initial version of cache api from plugins, usage:

// store json object in cache for 24 hours
plugin.cachePut("ItemStash", "ItemKey1", {
	title: 'Item title',
	thumb: 'http://localhost/thumb.jpg'
	}, 86400);

// fetch json object from cache
o = plugin.cacheGet("ItemStash", "ItemKey1");
  • Loading branch information...
1 parent 6f6ad04 commit e16e4595c00da4f1145632221e8db02351691b51 @hean01 hean01 committed Aug 21, 2012
Showing with 98 additions and 1 deletion.
  1. +2 −0 src/js/js.c
  2. +6 −0 src/js/js.h
  3. +90 −1 src/js/js_json.c
View
@@ -727,6 +727,8 @@ static JSFunctionSpec plugin_functions[] = {
JS_FS("createService", js_createService, 4, 0, 0),
JS_FS("getAuthCredentials", js_getAuthCredentials, 3, 0, 0),
JS_FS("onEvent", js_onEvent, 2, 0, 0),
+ JS_FS("cacheGet", js_cache_get, 2, 0, 0),
+ JS_FS("cachePut", js_cache_put, 4, 0, 0),
JS_FS_END
};
View
@@ -128,6 +128,12 @@ JSBool js_json_encode(JSContext *cx, JSObject *obj,
JSBool js_json_decode(JSContext *cx, JSObject *obj,
uintN argc, jsval *argv, jsval *rval);
+JSBool js_cache_put(JSContext *cx, JSObject *obj, uintN argc,
+ jsval *argv, jsval *rval);
+
+JSBool js_cache_get(JSContext *cx, JSObject *obj, uintN argc,
+ jsval *argv, jsval *rval);
+
struct http_auth_req;
int js_http_auth_try(const char *url, struct http_auth_req *har);
View
@@ -1,6 +1,7 @@
/*
* Fast JSPAI JSON encoder / decoder
* Copyright (C) 2011 Andreas Öman
+ * Copyright (C) 2012 Henrik Andersson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -22,7 +23,7 @@
#include "misc/dbl.h"
#include "misc/json.h"
#include "js.h"
-
+#include "blobcache.h"
static int json_encode_from_object(JSContext *cx, JSObject *obj,
htsbuf_queue_t *out);
@@ -312,3 +313,91 @@ js_json_decode(JSContext *cx, JSObject *obj,
}
return JS_TRUE;
}
+
+/**
+ *
+ */
+JSBool
+js_cache_put(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
+{
+ char stash[256];
+ const char *key,*lstash;
+ char *value;
+ uint32_t maxage;
+ JSObject *o;
+ htsbuf_queue_t out;
+ js_plugin_t *jsp = JS_GetPrivate(cx, obj);
+
+ if (!JS_ConvertArguments(cx, argc, argv, "ssou",
+ &lstash, &key, &o, &maxage))
+ return JS_FALSE;
+
+ if (o == NULL) {
+ JS_ReportError(cx, "Not an object");
+ return JS_FALSE;
+ }
+
+ // json encode object
+ htsbuf_queue_init(&out, 0);
+ if (json_encode_from_object(cx, o, &out) != 0) {
+ JS_ReportError(cx, "Not an JSON object");
+ return JS_FALSE;
+ }
+
+ value = malloc(out.hq_size);
+ value[out.hq_size] = '\0';
+ htsbuf_read(&out, value, out.hq_size);
+
+ // put json encoded object onto cache
+ snprintf(stash, sizeof(stash), "plugin/%s/%s", jsp->jsp_id, lstash);
+ TRACE(TRACE_DEBUG,"Storing %s into stash %s", value, stash);
+ blobcache_put(key, stash, value, strlen(value)+1, maxage, NULL, 0);
+
+ free(value);
+ return JS_TRUE;
+}
+
+/**
+ *
+ */
+JSBool
+js_cache_get(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
+{
+ void *value;
+ size_t vsize;
+ char stash[256];
+ char errbuf[256];
+ const char *key,*lstash;
+ JSObject *o;
+
+ js_plugin_t *jsp = JS_GetPrivate(cx, obj);
+
+ if (!JS_ConvertArguments(cx, argc, argv, "ss", &lstash, &key))
+ return JS_FALSE;
+
+ // fetch json from cache
+ snprintf(stash, sizeof(stash), "plugin/%s/%s", jsp->jsp_id, lstash);
+ value = blobcache_get(key, stash, &vsize, 0, NULL, NULL, NULL);
+
+ if(value == NULL) {
+ *rval = OBJECT_TO_JSVAL(NULL);
+ return JS_TRUE;
+ }
+
+ // deserialize into json object
+ if(!JS_EnterLocalRootScope(cx))
+ return JS_FALSE;
+
+ o = json_deserialize(value, &json_to_jsapi, cx, errbuf, sizeof(errbuf));
+
+ *rval = OBJECT_TO_JSVAL(o);
+
+ JS_LeaveLocalRootScope(cx);
+
+ if(o == NULL) {
+ JS_ReportError(cx, "Invalid JSON stored in cache -- %s", errbuf);
+ return JS_FALSE;
+ }
+
+ return JS_TRUE;
+}

0 comments on commit e16e459

Please sign in to comment.