Persistor = require './fs_persist'
CacheUpdater = require './updater'
Cache = require './basic'
md5 = require 'MD5'
Log = require '../../util/log'

class SiteCache
  constructor: (domain, @backend) ->
    throw new Error("Domain cannot be null") unless domain?
    throw new Error("Backend cannot be null") unless @backend?
    @cacheKey = domain;
    @updateQueue = [];

  init: (callback)->
    @persistor = new Persistor(@cacheKey);
    @cache = new Cache(@persistor);
    @initUpdater();
    @cache.init =>
      callback.apply @, []

  initUpdater: ()->
    @updater = new CacheUpdater(@, @backend);

  update: (url, comment, callback)->
    @updater.update url, callback

  has: (key)->
    @cache.has key

  get: (key)->
    @cache.get key

  getAll: ()->
    @cache.getAll()

  set: (key, val)->
    Log.debug "Caching with key #{key}."
    @cache.set key, val

  keyFor: (url)->
    return md5(url)

module.exports = SiteCache;