summaryrefslogtreecommitdiffstats
path: root/coffee/services/cache.coffee
blob: 194fca3f13c232660ef0a593d474828440adbaed (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
angular.module('News').factory '_Cache', ->

	class Cache

		constructor: (@feedType, @feedModel, @folderModel) ->
			@clear()


		clear: ->
			@feedCache = []
			@folderCache = {}
			@folderCacheLastModified = 0
			@importantCache = []
			@highestId = 0
			@lowestId = 0
			@highestTimestamp = 0
			@lowestTimestamp = 0
			@highestIds = {}
			@lowestIds = {}
			@highestTimestamps = {}
			@lowestTimestamps = {}


		add: (item) ->
			# cache for feed access
			if not @feedCache[item.feedId]
				@feedCache[item.feedId] = []
			@feedCache[item.feedId].push(item)
			
			# cache for non feeds
			if @highestTimestamp < item.date
				@highestTimestamp = item.date
			if @lowestTimestamp > item.date
				@lowestTimestamp = item.date
			if @highestId < item.id
				@highestId = item.id
			if @lowestId > item.id
				@lowestId = item.id


			# cache for important
			if item.isImportant
				@importantCache.push(item)

			# cache lowest and highest ids and timestamps for only fetching new
			# items
			if @highestTimestamps[item.feedId] == undefined or item.id > @highestTimestamps[item.feedId]
				@highestTimestamps[item.feedId] = item.date
			if @lowestTimestamps[item.feedId] == undefined or item.id > @lowestTimestamps[item.feedId]
				@lowestTimestamps[item.feedId] = item.date
			if @highestIds[item.feedId] == undefined or item.id > @highestIds[item.feedId]
				@highestIds[item.feedId] = item.id
			if @lowestIds[item.feedId] == undefined or item.id > @lowestIds[item.feedId]
				@lowestIds[item.feedId] = item.id


		getItemsOfFeed: (feedId) ->
			return @feedCache[feedId]


		getFeedIdsOfFolder: (folderId) ->
			@buildFolderCache(folderId)
			return @folderCache[folderId]


		getImportantItems: ->
			return @importantCache


		buildFolderCache: (id) ->
			# invalidate the foldercache if the last modified date is
			# not the currently used one
			if @folderCacheLastModified != @feedModel.getLastModified()
				@folderCache = {}
				@folderCacheLastModified = @feedModel.getLastModified()
			
			# if the folderarray does not yet exist, build it
			# otherwise use the last generated one
			if @folderCache[id] == undefined
				@folderCache[id] = []
				for feed in @feedModel.getItems()
					if feed.folderId == id
						@folderCache[id].push(feed.id)


		getFeedsOfFolderId: (id) ->
			@buildFolderCache(id)
			return @folderCache[id]


		removeItemInArray: (id, array) ->
			removeItemIndex = null
			counter = 0
			for element in array
				if element.id == id
					removeItemIndex = counter
					break
				counter += 1

			if removeItemIndex != null
				array.splice(removeItemIndex, 1)


		remove: (item) ->
			@removeItemInArray(item.id, @feedCache[item.feedId])
			@removeItemInArray(item.id, @importantCache)


		setImportant: (item, isImportant) ->
			if isImportant
				@importantCache.push(item)
			else
				@removeItemInArray(item.id, @importantCache)
			
		getHighestId: (type, id) ->
			if @isFeed(type)
				return @highestIds[id] || 0
			else
				return @highestId


		getHighestTimestamp: (type, id) ->
			if @isFeed(type)
				return @highestTimestamps[id] || 0
			else
				return @highestTimestamp


		getLowestId: (type, id) ->
			if @isFeed(type)
				return @lowestIds[id] || 0
			else
				return @lowestId


		getLowestTimestamp: (type, id) ->
			if @isFeed(type)
				return @lowestTimestamps[id] || 0
			else
				return @lowestTimestamp


		isFeed: (type) ->
			return type == @feedType.Feed


	return Cache