Skip to content

Cache helpers

cache(pkg_id)

Adds the package id and current time to the 'twitter' cache. Clears any existing entries for the given package id first.

Parameters:

Name Type Description Default
pkg_id

The package id to store.

required
Source code in ckanext/twitter/lib/cache_helpers.py
19
20
21
22
23
24
25
26
27
def cache(pkg_id):
    """
    Adds the package id and current time to the 'twitter' cache. Clears any existing
    entries for the given package id first.

    :param pkg_id: The package id to store.
    """
    twitter_cache.remove_value(pkg_id)
    twitter_cache.put(pkg_id, dt.now())

expired(pkg_id)

Checks to see if the cache entry for the package's last tweet (if any) is old enough to be overwritten.

Parameters:

Name Type Description Default
pkg_id

The package ID.

required

Returns:

Type Description

boolean

Source code in ckanext/twitter/lib/cache_helpers.py
48
49
50
51
52
53
54
55
56
57
58
59
60
def expired(pkg_id):
    """
    Checks to see if the cache entry for the package's last tweet (if any) is old enough
    to be overwritten.

    :param pkg_id: The package ID.
    :return: boolean
    """
    if not twitter_cache.has_key(pkg_id):
        return True
    last_posted = twitter_cache.get(pkg_id)
    hours_since = (dt.now() - last_posted).seconds / 3600
    return hours_since > config_helpers.twitter_hours_between_tweets()

remove_from_cache(pkg_id)

Remove the package id from the cache.

Parameters:

Name Type Description Default
pkg_id
required
Source code in ckanext/twitter/lib/cache_helpers.py
39
40
41
42
43
44
45
def remove_from_cache(pkg_id):
    """
    Remove the package id from the cache.

    :param pkg_id:
    """
    twitter_cache.remove_value(pkg_id)

reset_cache()

Clears everything from the 'twitter' cache.

Source code in ckanext/twitter/lib/cache_helpers.py
30
31
32
33
34
35
36
def reset_cache():
    """
    Clears everything from the 'twitter' cache.
    """
    twitter_cache.clear()
    for k, c in cache_managers.items():
        c.clear()