Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Zahra Rajabi
tweepy
Commits
517031b2
Commit
517031b2
authored
Nov 14, 2012
by
Joshua Roesslein
Browse files
Merge pull request #221 from wladston/master
Added a simple MongoDB cache backend.
parents
cf579f04
2e7638e2
Changes
1
Hide whitespace changes
Inline
Side-by-side
tweepy/cache.py
View file @
517031b2
...
...
@@ -3,6 +3,7 @@
# See LICENSE for details.
import
time
import
datetime
import
threading
import
os
...
...
@@ -381,3 +382,43 @@ class RedisCache(Cache):
keys
=
self
.
client
.
smembers
(
self
.
keys_container
)
for
key
in
keys
:
self
.
delete_entry
(
key
)
class
MongodbCache
(
Cache
):
"""A simple pickle-based MongoDB cache sytem."""
def
__init__
(
self
,
db
,
timeout
=
3600
,
collection
=
'tweepy_cache'
):
"""Should receive a "database" cursor from pymongo."""
Cache
.
__init__
(
self
,
timeout
)
self
.
timeout
=
timeout
self
.
col
=
db
[
collection
]
self
.
col
.
create_index
(
'created'
,
expireAfterSeconds
=
timeout
)
def
store
(
self
,
key
,
value
):
from
bson.binary
import
Binary
now
=
datetime
.
datetime
.
utcnow
()
blob
=
Binary
(
pickle
.
dumps
(
value
))
self
.
col
.
insert
({
'created'
:
now
,
'_id'
:
key
,
'value'
:
blob
})
def
get
(
self
,
key
,
timeout
=
None
):
if
timeout
:
raise
NotImplementedError
obj
=
self
.
col
.
find_one
({
'_id'
:
key
})
if
obj
:
return
pickle
.
loads
(
obj
[
'value'
])
def
count
(
self
):
return
self
.
col
.
find
({}).
count
()
def
delete_entry
(
self
,
key
):
return
self
.
col
.
remove
({
'_id'
:
key
})
def
cleanup
(
self
):
"""MongoDB will automatically clear expired keys."""
pass
def
flush
(
self
):
self
.
col
.
drop
()
self
.
col
.
create_index
(
'created'
,
expireAfterSeconds
=
self
.
timeout
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment