Client Side Caching Library

    I looked for a post about this library on the hub, I didn’t find it and decided to write a little about it.
    The library is called locache.js and allows you to cache JS strings, arrays, and objects. The highlight of the library is that you can cache as inside a user session, i.e. before rebooting the browser window, and beyond its limits, i.e. even after closing the browser, the cache will remain.

    A couple of examples from the library site:

    1. // Set The Cache Lifetime
    2. var seconds = 60;
    4. // Write data to the cache and set the lifetime to 60 seconds
    5. locache.set ("key", {
    6.     'user': 1, 
    7.     'books': ['a', 'b', 'c']
    8. }, seconds);
    10. // Get data from the cache
    11. locache.get ("key");
    12. // {'user': 1, 'books': ['a', 'b', 'c']}
    13. // Attention, the object is returned, not the string
    15. // Eden 60 seconds and again try to get data from the cache
    16. locache.get ("key");
    17. // null
    19. // If you want to save data only for the current session
    20. // use the following entry
    21. locache.session.set ("private", {
    22.     'likes': ['kittens', 'JavaScript']
    23. });
    25. // All other methods also work for writing .session
    26. locache.session.get ("private");
    28. // All data stored in the session will be lost
    29. // when the user closes the browser

    In addition, I will give a real example of using this library. Now I am working on a web-based interface that loads only once and then all the actions create only AJAX requests to the server to receive new data. The interface and its loadable modules have many settings that change quite rarely. I store such settings as static files with a string representation of JS objects. With each change, the file gets a unique name. Then these files are read by the interface using AJAX requests. To cache them by file name, I use this library.

    I hope someone comes in handy just like me.

    Also popular now: