~ubuntu-branches/ubuntu/trusty/pitivi/trusty

« back to all changes in this revision

Viewing changes to pitivi/thumbnailcache.py

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2014-04-05 15:28:16 UTC
  • mfrom: (6.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20140405152816-6lijoax4cngiz5j5
Tags: 0.93-3
* debian/control:
  + Depend on python-gi (>= 3.10), older versions do not work
    with pitivi (Closes: #732813).
  + Add missing dependency on gir1.2-clutter-gst-2.0 (Closes: #743692).
  + Add suggests on gir1.2-notify-0.7 and gir1.2-gnomedesktop-3.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# PiTiVi , Non-linear video editor
2
 
#
3
 
#       thumbnailcache.py
4
 
#
5
 
# Copyright (c) 2009, Brandon Lewis (brandon_lewis@berkeley.edu)
6
 
#
7
 
# This program is free software; you can redistribute it and/or
8
 
# modify it under the terms of the GNU Lesser General Public
9
 
# License as published by the Free Software Foundation; either
10
 
# version 2.1 of the License, or (at your option) any later version.
11
 
#
12
 
# This program is distributed in the hope that it will be useful,
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
# Lesser General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU Lesser General Public
18
 
# License along with this program; if not, write to the
19
 
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20
 
# Boston, MA 02110-1301, USA.
21
 
 
22
 
"""
23
 
Dictionary-Like object for caching of thumbnails.
24
 
"""
25
 
 
26
 
import collections
27
 
 
28
 
 
29
 
class ThumbnailCache(object):
30
 
 
31
 
    """Caches thumbnails by key using LRU policy, implemented with heapq"""
32
 
 
33
 
    def __init__(self, size=100):
34
 
        object.__init__(self)
35
 
        self.queue = collections.deque()
36
 
        self.cache = {}
37
 
        self.hits = 0
38
 
        self.misses = 0
39
 
        self.size = size
40
 
 
41
 
    def __contains__(self, key):
42
 
        if key in self.cache:
43
 
            self.hits += 1
44
 
            return True
45
 
        self.misses += 1
46
 
        return False
47
 
 
48
 
    def __getitem__(self, key):
49
 
        if key in self.cache:
50
 
            # I guess this is why LRU is considered expensive
51
 
            self.queue.remove(key)
52
 
            self.queue.append(key)
53
 
            return self.cache[key]
54
 
        raise KeyError(key)
55
 
 
56
 
    def __setitem__(self, key, value):
57
 
        self.cache[key] = value
58
 
        self.queue.append(key)
59
 
        if len(self.cache) > self.size:
60
 
            self.ejectLRU()
61
 
 
62
 
    def ejectLRU(self):
63
 
        key = self.queue.popleft()
64
 
        del self.cache[key]