This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cacheServiceAdapters.js
109 lines (99 loc) · 3.21 KB
/
cacheServiceAdapters.js
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
import angular from 'angular'
export default cacheAdaptersConfig
cacheAdaptersConfig.$inject = ['httpEtagProvider']
function cacheAdaptersConfig (httpEtagProvider) {
httpEtagProvider
.defineCacheServiceAdapter('$cacheFactory', {
config: {
storesDeepCopies: false
},
methods: {
createCache: function createCache ($cacheFactory, cacheId, options) {
$cacheFactory(cacheId, options)
},
getCache: function getCache ($cacheFactory, cacheId) {
return $cacheFactory.get(cacheId)
},
setItem: function setItem (cache, itemKey, value) {
cache.put(itemKey, value)
},
getItem: function getItem (cache, itemKey) {
return cache.get(itemKey)
},
removeItem: function removeItem (cache, itemKey) {
cache.remove(itemKey)
},
removeAllItems: function removeAllItems (cache, itemKey) {
cache.removeAll()
}
}
})
.defineCacheServiceAdapter('localStorage', {
config: {
storesDeepCopies: true
},
methods: {
createCache: angular.noop,
getCache: function getCache (localStorage, cacheId) {
return cacheId
},
setItem: function setItem (cacheId, itemKey, value) {
try {
itemKey = cacheId + ':' + itemKey
localStorage.setItem(itemKey, JSON.stringify(value))
} catch (e) {
}
},
getItem: function getItem (cacheId, itemKey) {
itemKey = cacheId + ':' + itemKey
return JSON.parse(localStorage.getItem(itemKey))
},
removeItem: function removeItem (cacheId, itemKey) {
itemKey = cacheId + ':' + itemKey
localStorage.removeItem(itemKey)
},
removeAllItems: function removeAllItems (cacheId, itemKey) {
var keyPrefix = cacheId + ':'
angular.forEach(localStorage, function (value, key) {
if (key.indexOf(keyPrefix) === 0) {
localStorage.removeItem(key)
}
})
}
}
})
.defineCacheServiceAdapter('sessionStorage', {
config: {
storesDeepCopies: true
},
methods: {
createCache: angular.noop,
getCache: function getCache (sessionStorage, cacheId) {
return cacheId
},
setItem: function setItem (cacheId, itemKey, value) {
try {
itemKey = cacheId + ':' + itemKey
sessionStorage.setItem(itemKey, JSON.stringify(value))
} catch (e) {
}
},
getItem: function getItem (cacheId, itemKey) {
itemKey = cacheId + ':' + itemKey
return JSON.parse(sessionStorage.getItem(itemKey))
},
removeItem: function removeItem (cacheId, itemKey) {
itemKey = cacheId + ':' + itemKey
sessionStorage.removeItem(itemKey)
},
removeAllItems: function removeAllItems (cacheId, itemKey) {
var keyPrefix = cacheId + ':'
angular.forEach(sessionStorage, function (value, key) {
if (key.indexOf(keyPrefix) === 0) {
sessionStorage.removeItem(key)
}
})
}
}
})
}