summaryrefslogtreecommitdiff
path: root/lib/feed2imap/cache.rb
diff options
context:
space:
mode:
authorlnu <lnu@f70e237a-67f3-0310-a06c-d2b8a7116972>2005-04-21 20:54:26 +0000
committerlnu <lnu@f70e237a-67f3-0310-a06c-d2b8a7116972>2005-04-21 20:54:26 +0000
commit02a13d1619058b2a73a5040c24d80f3caeb01ed9 (patch)
tree88a97011dd9cb581a3af5ffa8f2502581294e5d7 /lib/feed2imap/cache.rb
parentc464d5c97bdbe326ed1b259dacad3c22d6623c54 (diff)
downloadfeed2imap-02a13d1619058b2a73a5040c24d80f3caeb01ed9.tar.gz
feed2imap-02a13d1619058b2a73a5040c24d80f3caeb01ed9.tar.bz2
feed2imap-02a13d1619058b2a73a5040c24d80f3caeb01ed9.zip
git-svn-id: svn+ssh://svn.gna.org/svn/feed2imap/trunk/feed2imap@21 f70e237a-67f3-0310-a06c-d2b8a7116972
Diffstat (limited to 'lib/feed2imap/cache.rb')
-rw-r--r--lib/feed2imap/cache.rb45
1 files changed, 35 insertions, 10 deletions
diff --git a/lib/feed2imap/cache.rb b/lib/feed2imap/cache.rb
index c984a5d..48913f0 100644
--- a/lib/feed2imap/cache.rb
+++ b/lib/feed2imap/cache.rb
@@ -35,10 +35,10 @@ class ItemCache
return @channels[id].get_new_items(items)
end
- # Replace the existing cached items by those ones
- def update_cache(id, items)
+ # Commit changes to the cache
+ def commit_cache(id)
@channels[id] ||= CachedChannel::new
- @channels[id].update(items)
+ @channels[id].commit
end
# Get the last time the cache was updated
@@ -91,18 +91,36 @@ class ItemCache
end
class CachedChannel
+ # Size of the cache for each feed
+ CACHESIZE = 50
+
attr_accessor :lastcheck, :items
def initialize
@lastcheck = Time::at(0)
@items = []
+ @itemstemp = [] # see below
+ @nbnewitems = 0
end
+ # Let's explain @items and @itemstemp.
+ # @items contains the CachedItems serialized to the disk cache.
+ # The - quite complicated - get_new_items method fills in @itemstemp
+ # but leaves @items unchanged.
+ # Later, the commit() method replaces @items with @itemstemp and
+ # empties @itemstemp. This way, if something wrong happens during the
+ # upload to the IMAP server, items aren't lost.
+ # @nbnewitems is set by get_new_items, and is used to limit the number
+ # of (old) items serialized.
+
# Returns the really new items amongst items
def get_new_items(items)
+ # save number of new items
+ @nbnewitems = items.length
# set items' cached version if not set yet
newitems = []
updateditems = []
+ @itemstemp = @items
items.each { |i| i.cacheditem ||= CachedItem::new(i) }
items.each do |i|
found = false
@@ -111,6 +129,9 @@ class CachedChannel
if i.cacheditem == j
i.cacheditem.index = j.index
found = true
+ # let's put j in front of itemstemp
+ @itemstemp.delete(j)
+ @itemstemp.unshift(j)
break
end
end
@@ -123,6 +144,9 @@ class CachedChannel
i.cacheditem.updated = true
updateditems.push(i)
found = true
+ # let's put j in front of itemstemp
+ @itemstemp.delete(j)
+ @itemstemp.unshift(j)
break
end
end
@@ -130,16 +154,17 @@ class CachedChannel
# add as new
i.cacheditem.create_index
newitems.push(i)
+ # add i.cacheditem to @itemstemp
+ @itemstemp.unshift(i.cacheditem)
end
return [newitems, updateditems]
end
-
- # Replace the existing cached items by those ones
- def update(items)
- @items = []
- items.each do |i|
- @items.push(i.cacheditem)
- end
+
+ def commit
+ # too old items must be dropped
+ n = @nbnewitems > CACHESIZE ? @nbnewitems : CACHESIZE
+ @items = @itemstemp[0..n]
+ @itemstemp = []
self
end