cache-linked-list

1.0.0 • Public • Published

cache-linked-list

Cache items from list where the next/prev item is obtained through last/first item.

Usage

See example folder for more details.

// create cache that caches 2 more items than requested
// on each side of the requested range
// and keeps 3 more items on the end opposite
// to the end being updated (hysteresis)

let cache = new CacheLinkedList(2, 3)
cache.nextItem = function (last) { //should return a promise
  if (!last) return ajax(makeSearchUrl(searchTerm)).get() //get first item
  if (!last.nextPageToken) return Promise.resolve() //no next item
  return ajax(makeSearchUrl(searchTerm, last.nextPageToken)).get() //promise for next
}
cache.prevItem = function (first) {
  if (!first.prevPageToken) return Promise.resolve() //no prev item
  return ajax(makeSearchUrl(searchTerm, first.prevPageToken)).get()
}
cache.onfinish = displayItems
cache.onitem = (item, index) => console.log('onitem ' + index)


cache.update([0, 3]) //request items from 0 to 3 including
//items -2..5 will be fetched if possible
...
cache.update([6, 7])
//items 4..9 are needed (6-2, 7+2)
//items 0..5 are available
//items 6..9 will be fetched
//items 1..3 will be kept due to hysteresis
//item 0 will be removed from cache
...
cache.update([4, 6])
//items 2..8 are needed
//items 1..9 are available
//no fetching of new items needed
//item 9 will be kept due to hysteresis
//no items will be removed

Readme

Keywords

none

Package Sidebar

Install

npm i cache-linked-list

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • grabantot