All files / src index.js

100% Statements 35/35
85.71% Branches 18/21
100% Functions 7/7
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                      14x     14x     14x       14x       8x   5x   5x   1x 1x     4x 4x     5x           3x   3x 5x         3x   3x   1x 1x   2x 2x     3x                       2x   1x           1x   2x   1x                   2x   2x   1x         1x   2x   1x                
import setItem from './operations/set_item'
import getItem from './operations/get_item'
import removeItem from './operations/remove_item'
import Item from './util/item'
import Logger from './util/logger'
 
import session from './storage/session_storage'
import local from './storage/local_storage'
 
export default class SimpleCache {
  constructor (opts = {}) {
    this.ttl = opts.ttl
      ? opts.ttl
      : 1000 * 60 * 60 * 24 // 24 hour default
    this.namespace = opts.namespace
      ? opts.namespace
      : 'SimpleCache:'
    this.log = Logger.create({
      debug: opts.debug,
      key: this.namespace
    })
    this.buildItem = Item.create(this.ttl, this.log).build
  }
 
  set (...args) {
    if (typeof args[0] === 'string') {
      // Single Set
      let store = null
 
      if (args[2]) {
        // We're using Session Storage
        this.log(`Storing in Session`)
        store = session
      } else {
        // We're using Local Storage
        this.log(`Storing in Local`)
        store = local
      }
 
      return setItem.single({
        key: `${this.namespace}${args[0]}`,
        value: this.buildItem(args[1]),
        store,
        log: this.log
      })
    } else Eif (Array.isArray(args[0])) {
      // Multi Set
      const values = args[0].map(({ key, value }) => {
        return {
          key,
          value: this.buildItem(value)
        }
      })
      let store = null
 
      if (args[1]) {
        // We're using session storage
        this.log(`Storing in Session`)
        store = session
      } else {
        this.log(`Storing in Local`)
        store = local
      }
 
      return setItem.multi({
        namespace: this.namespace,
        values,
        store,
        log: this.log
      })
    }
  }
 
  get (...args) {
    // NOTE: I don't love this but I need to pass in the stores
    // explicitly. I'll come back and do this is a cleaner way.
    if (typeof args[0] === 'string') {
      // Single Retrieval
      return getItem.single({
        key: `${this.namespace}${args[0]}`,
        local,
        session,
        log: this.log
      })
    } else Eif (Array.isArray(args[0])) {
      // Multi Removal
      const keys = args[0].map(key => `${this.namespace}${key}`)
 
      return getItem.multi({
        keys,
        local,
        session,
        log: this.log
      })
    }
  }
 
  remove (...args) {
    const stores = [ session, local ]
 
    if (typeof args[0] === 'string') {
      // Single Removal
      return removeItem.single({
        key: `${this.namespace}${args[0]}`,
        stores,
        log: this.log
      })
    } else Eif (Array.isArray(args[0])) {
      // Multi Removal
      const keys = args[0].map(key => `${this.namespace}${key}`)
 
      return removeItem.multi({
        keys,
        stores,
        log: this.log
      })
    }
  }
}