📜 ⬆️ ⬇️

Library for client side caching

I looked for a post on this library about Habré, did not find it, and decided to write a short story 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 within a user session, i.e. before the browser window is reloaded, or behind it, i.e. even after the browser is closed, the cache will remain.



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


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

I hope someone will find it useful just like me.

Source: https://habr.com/ru/post/207884/


All Articles