repo.or.cz
/
trackgit.git
/
blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
log
|
graphiclog1
|
graphiclog2
|
commit
|
commitdiff
|
tree
|
refs
|
edit
|
fork
blame
|
history
|
raw
|
HEAD
Use the right lookup in dbcache.Cache.__contains__
[trackgit.git]
/
dbcache.py
blob
d048170305d399435baeb719ff64c81ee2f31c1c
1
class
Cache
(
object
):
2
3
def
__init__
(
self
,
cls
,
attr
,
session
):
4
self
.
cls
=
cls
5
self
.
attr
=
attr
6
self
._
cache
= {}
7
self
.
session
=
session
8
9
def
__getitem__
(
self
,
key
):
10
try
:
11
return
self
._
cache
[
key
]
12
except
KeyError
:
13
pass
14
v
=
self
.
session
.
query
(
self
.
cls
).
filter
(
self
.
attr
==
key
).
first
()
15
if not
v
:
16
raise
KeyError
17
self
._
cache
[
key
] =
v
18
return
v
19
20
def
__contains__
(
self
,
key
):
21
try
:
22
v
=
self
[
key
]
23
except
KeyError
:
24
return False
25
return True
26
27
def
get
(
self
,
key
):
28
try
:
29
v
=
self
[
key
]
30
except
KeyError
:
31
v
=
self
.
cls
(
key
)
32
self
._
cache
[
key
] =
v
33
self
.
session
.
add
(
v
)
34
return
v