Monday, November 13th, 2006
JSOC: JavaScript Object Cache
Fancy memcached in your client browsers? Introducing: JSOC (JavaScript Object Cache).
The JSOC framework is a a pluggable, extensible, open source client-side caching framework for JavaScript.
JSOC offers Web developers a straightforward way to perform common caching techniques (add, replace, remove, flush, etc.) inside any JavaScript-enabled browser.
Since JSOC is a standalone JavaScript module, incorporating JSOC into a Web development project is a matter of including a script reference, and working with common caching methods. Low-level methods are contained in the JSOC JavaScript module so that developers can focus on the Web development task at hand.
Example
-
-
xhttp = new xhttp(); // use your XMLHTTPRequest library of choice!
-
function setCache(n,v){
-
var respCode = jsoc.set(n, v);
-
alert('Data containing ' + n + ' was fetched and cached!');
-
}
-
-
function clearCacheItem(n){
-
var respCode = jsoc.remove(n);
-
alert('Data containing ' + n + ' is no longer cached.');
-
}
-
-
function cacheStuff(cName){
-
jsoc = new JSOC(); // JavaScript Object Cache
-
if(jsoc.get(cName) == undefined){
-
// this object is not cached, so grab it as usual, then cache it via your call-back function.
-
cacheName = cName; // set the global cacheName so that it's in scope for the call-back function.
-
loc = 'http://dev.webframeworks.com/assets/getMyData.txt';
-
xhttp.fetch(loc, 'responseProxy', {'method':'POST','type':'text'});
-
}else{
-
// grab your stuff from cache!
-
var cacheItem = eval(cName);
-
alert(cName + ' = ' + cacheItem);
-
}
-
}
-
-
function responseProxy(response){
-
var x = eval("("+ response+")");
-
setCache(cacheName, cacheName, x.articles[17].article.body); // this is where cache is set as mentioned above.
-
}
-












Very nice piece of code.
By the way: CSS got messed up on this webpage? :\
To be on the safe side, I’ve taken a screenshot as proof. I’m nasty :\
Maybe someone of the developer team of this site wants to have a look at it for troubelshooting?
this is big! hardcore developers are going to go crazy with this one. let the games being!
looks like the Ajaxian crew was so excited when they posted this one, a tag or two got messed up. It is monday afterall. :)
Not the finest piece of engineering. Could you rewrite that without the eval:s, and with descriptive parameter names?
I get suspicious of code which uses eval for variable lookup, it’s definitely a smell. Use associative arrays instead, that is what they are for.
It’s about time someone put this together.
Less tripping over to the server spells RELIEF and
I’m glad the global variable “keys” is now taken over by JSOC… </sarcasm>
Yeah. That example code is pretty awful. It’s broken — it can’t do concurrency and it has no queue to protect it from itself. Example code like this shouldn’t be published, period. It makes the web stupider.
Very nice piece of code! Thanks, dude!
A very bloated way of stuffing the global scope full of junk, too. The example would be less horrible if it kept its object cache in its own JSOC singleton, though of course still not allow concurrent use by more than one consumer.
Is there a library for persistent cache ? Can it be done without installing software on the browser .
This is cool …
Globals? WTF?
It’s not the worst JavaScript code I’ve ever seen, but it’s close.
My jaw is still dropping.
This is not true. It can’t be true.
I won’t believe it.
N wonder he’s hiding behind his baseball cap…
Link Listing - November 13, 2006
Advanced .NET Master Class next month…win a free seat! [Via: gduthie ] SharePoint Solution Generator…
Tobie, want to know what is even scarier?
http://www.google.com/search?q=javascript+title+case
Yes, it’s #1 out of three million!
It is nice way to minimize traffic in AJAX apps! =-)
… jaw hits floor.
Links (15 Oct), Web, LUA, .NET, Office 2007 Stuff
General/Web Stuff Windows Vista Ships with .NET FX 3.0 and IIS7 Built-in [Via: ScottGu ] Sun open sources
Well, I just looked at their 0.0.13 code that was released today. It’s not nearly as bad as before—they got rid of the accidental globals—but the code looks like it was written by somebody that is just learning JavaScript. A good 25% of the code consists of simple mistakes that an experienced JavaScript programmer wouldn’t make.
Now there’s nothing wrong with being a JavaScript beginner—obviously we all were at one time—but it’s quite a stretch to call this “a pluggable, extensible, open source client-side caching framework for JavaScript”.
Oh no. I just read their docs. Guys, this is not JSON you’re working with. It has nothing to do with JSON.
s/JSON/JavaScript/ and you’ll be OK.
It’s worse than I thought. Compare their implementations of
set(),add(), andreplace(). They are the same 20-line function, with one or two lines different at the top!I don’t get it, what’s wrong with that Javascript at http://www.madirish.net/tech.php?section=1&article=6 ;) Thanks for pointing that out though, I wrote the original code at least 5 years ago (which is why it’s a #1 Google hit) when I was first learning Javascript, and it was in some serious need of updating.
[...] JSOC: JavaScript Object Cache, dica muito, muito útil. [...]