Debian Codegraph @

docs/quickstart.rst

raw
1 Quickstart
2 ==========
3
4 Eager to get started? This page gives a good introduction to CacheLib.
5 Follow :doc:`installation` to set up a project and install CacheLib first.
6
7
8 A Minimal Example
9 -----------------
10
11 A minimal example of using :class:`.SimpleCache` from CacheLib
12 looks like this:
13
14 .. code-block:: python
15
16 from cachelib import SimpleCache
17
18 # Create a cache instance
19 cache = SimpleCache()
20
21 # Set a value in the cache
22 cache.set('my_key', 'my_value')
23
24 # Retrieve the value from the cache
25 value = cache.get('my_key')
26 print(value) # Output: my_value
27
28 # Delete the value from the cache
29 cache.delete('my_key')
30
31 # Try to retrieve the deleted value
32 value = cache.get('my_key')
33 print(value) # Output: None
34
35 The code above does the following:
36
37 1. Import the :class:`.SimpleCache` class from the ``cachelib`` module.
38 2. Create a cache instance using ``cache = SimpleCache()``.
39 3. Set a value in the cache with the key ``'my_key'`` and the value
40 ``'my_value'`` using the :meth:`.SimpleCache.set` method.
41 4. Retrieve the value from the cache using the
42 :meth:`.SimpleCache.get` method.
43 5. Print the retrieved value, which outputs ``my_value``.
44 6. Delete the value from the cache using the
45 :meth:`.SimpleCache.delete` method.
46 7. Retrieve the deleted value again, which returns ``None`` since it has
47 been removed from the cache.
48
49 This example uses the :class:`.SimpleCache` backend. CacheLib
50 supports multiple backends, such as :class:`.RedisCache`,
51 :class:`.MemcachedCache`, and more. Each backend is used
52 the same way by creating an instance of the desired cache class.
53
54
55 To run the application, use ``python quickstart.py``.
56 The output shows the retrieved value and ``None`` for the deleted key.
57
58 .. code-block:: console
59
60 $ python quickstart.py
61 my_value
62 None