2018-01-28 20:51:48 +00:00
|
|
|
import QuickLRU from 'quick-lru'
|
|
|
|
|
|
|
|
export const statusesCache = {
|
|
|
|
maxSize: 100,
|
|
|
|
caches: {}
|
|
|
|
}
|
|
|
|
export const accountsCache = {
|
|
|
|
maxSize: 50,
|
|
|
|
caches: {}
|
|
|
|
}
|
|
|
|
export const relationshipsCache = {
|
|
|
|
maxSize: 20,
|
|
|
|
caches: {}
|
|
|
|
}
|
|
|
|
export const metaCache = {
|
|
|
|
maxSize: 20,
|
|
|
|
caches: {}
|
|
|
|
}
|
2018-02-04 02:06:02 +00:00
|
|
|
export const notificationsCache = {
|
|
|
|
maxSize: 50,
|
|
|
|
caches: {}
|
|
|
|
}
|
2018-01-28 20:51:48 +00:00
|
|
|
|
|
|
|
if (process.browser && process.env.NODE_ENV !== 'production') {
|
|
|
|
window.cacheStats = {
|
|
|
|
statuses: statusesCache,
|
|
|
|
accounts: accountsCache,
|
|
|
|
relationships: relationshipsCache,
|
2018-02-04 02:06:02 +00:00
|
|
|
meta: metaCache,
|
|
|
|
notifications: notificationsCache
|
2018-01-28 20:51:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
function getOrCreateInstanceCache (cache, instanceName) {
|
2018-01-28 20:51:48 +00:00
|
|
|
let cached = cache.caches[instanceName]
|
|
|
|
if (!cached) {
|
|
|
|
cached = cache.caches[instanceName] = new QuickLRU({maxSize: cache.maxSize})
|
|
|
|
}
|
|
|
|
return cached
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export function clearCache (cache, instanceName) {
|
2018-01-28 20:51:48 +00:00
|
|
|
delete cache.caches[instanceName]
|
|
|
|
}
|
2018-02-09 06:29:29 +00:00
|
|
|
export function setInCache (cache, instanceName, key, value) {
|
2018-01-28 20:51:48 +00:00
|
|
|
let instanceCache = getOrCreateInstanceCache(cache, instanceName)
|
|
|
|
return instanceCache.set(key, value)
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export function getInCache (cache, instanceName, key) {
|
2018-01-28 20:51:48 +00:00
|
|
|
let instanceCache = getOrCreateInstanceCache(cache, instanceName)
|
|
|
|
return instanceCache.get(key)
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export function hasInCache (cache, instanceName, key) {
|
2018-01-28 20:51:48 +00:00
|
|
|
let instanceCache = getOrCreateInstanceCache(cache, instanceName)
|
|
|
|
let res = instanceCache.has(key)
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
if (res) {
|
|
|
|
cache.hits = (cache.hits || 0) + 1
|
|
|
|
} else {
|
|
|
|
cache.misses = (cache.misses || 0) + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
2018-02-09 06:29:29 +00:00
|
|
|
}
|
2018-02-17 03:38:21 +00:00
|
|
|
|
|
|
|
export function deleteFromCache (cache, instanceName, key) {
|
|
|
|
let instanceCache = getOrCreateInstanceCache(cache, instanceName)
|
|
|
|
instanceCache.delete(key)
|
|
|
|
}
|