~ubuntu-branches/debian/sid/python-launchpadlib/sid

« back to all changes in this revision

Viewing changes to launchpadlib/docs/caching.txt

  • Committer: Bazaar Package Importer
  • Author(s): James Westby
  • Date: 2008-10-07 13:23:46 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20081007132346-e15qze2oiwwvko11
Tags: 0.2~bzr25-0ubuntu1
* New upstream snapshot.
  - Add support for hosted file resources, e.g. bug attachments.
  - Add support for indexing of collections that aren't top-level
    collections, so you don't have to slice and then index.
  - For hosted file resources expose filename and date uploaded.
    Take filename as an argument when uploading a file.
  - Automatically encode non-string parameters to named operations as JSON.
* Increase dependency on python-httplib2 to (>= 0.4.0) as the new version
  uses some of its new features.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Launchpadlib automatically decompresses the documents it receives, and
 
2
caches the responses in a temporary directory.
 
3
 
 
4
    >>> import httplib2
 
5
    >>> httplib2.debuglevel = 1
 
6
 
 
7
    >>> from launchpadlib.testing.helpers import salgado_with_full_permissions
 
8
    >>> launchpad_with_cache = salgado_with_full_permissions.login()
 
9
    connect: ...
 
10
    send: 'GET /beta/ ...
 
11
    reply: ...200...
 
12
    ...
 
13
    header: Transfer-Encoding: deflate
 
14
    ...
 
15
    send: 'GET /beta/ ...
 
16
    ...
 
17
    reply: ...200...
 
18
    ...
 
19
    header: Transfer-Encoding: deflate
 
20
    ...
 
21
 
 
22
    >>> print launchpad_with_cache.projects['firefox'].name
 
23
    send: 'GET /beta/firefox ...
 
24
    reply: ...200...
 
25
    ...
 
26
    firefox
 
27
 
 
28
The second and subsequent times you request some object, it's likely
 
29
that launchpadlib will make a conditional HTTP GET request instead of
 
30
a normal request. The HTTP response code will be 304 instead of 200,
 
31
and launchpadlib will use the cached representation of the object.
 
32
 
 
33
    >>> print launchpad_with_cache.projects['firefox'].name
 
34
    send: 'GET /beta/firefox ...
 
35
    reply: ...304...
 
36
    ...
 
37
    firefox
 
38
 
 
39
The default launchpadlib cache directory is a temporary directory
 
40
that's deleted when the Python process ends. (If the process is
 
41
killed, the directory will stick around in /tmp.) It's much more
 
42
efficient to keep a cache directory across multiple uses of
 
43
launchpadlib.
 
44
 
 
45
You can provide a cache directory name as argument when creating a
 
46
Launchpad object. This directory will fill up with cached HTTP
 
47
responses, and since it's a directory you control it will persist
 
48
across launchpadlib sessions.
 
49
 
 
50
    >>> import tempfile
 
51
    >>> tempdir = tempfile.mkdtemp()
 
52
 
 
53
    >>> first_launchpad = salgado_with_full_permissions.login(tempdir)
 
54
    connect: ...
 
55
    send: 'GET /beta/ ...
 
56
    reply: ...200...
 
57
    ...
 
58
    send: 'GET /beta/ ...
 
59
    reply: ...200...
 
60
    ...
 
61
 
 
62
    >>> print first_launchpad.projects['firefox'].name
 
63
    send: 'GET /beta/firefox ...
 
64
    reply: ...200...
 
65
    ...
 
66
    firefox
 
67
 
 
68
This will save you a *lot* of time in subsequent sessions, because
 
69
you'll be able to use cached versions of the initial (very expensive)
 
70
documents.
 
71
 
 
72
    >>> second_launchpad = salgado_with_full_permissions.login(tempdir)
 
73
    connect: ...
 
74
    send: 'GET /beta/ ...
 
75
    reply: ...304...
 
76
    ...
 
77
    send: 'GET /beta/ ...
 
78
    reply: ...304...
 
79
    ...
 
80
 
 
81
    >>> print second_launchpad.projects['firefox'].name
 
82
    send: 'GET /beta/firefox ...
 
83
    reply: ...304...
 
84
    ...
 
85
    firefox
 
86
 
 
87
Of course, if you ever need to clear the cache directory, you'll have
 
88
to do it yourself.
 
89
 
 
90
    >>> httplib2.debuglevel = 0
 
91
    >>> import shutil
 
92
    >>> shutil.rmtree(tempdir)