Skip to main content

Provides caching decorator to help speedup development

Project description

# devcache A configurable decorator allows methods to return persistently stored data from a cache instead of a normal call.

The use case is to speed up development by caching data from long-running methods

Installation

pip install devcache

Situation

You're working on a project that syncs data from a Database to a CRM.

def get_crm_data():  # Takes multiple minutes
    ...

def get_db_data():  # Takes multiple minutes
    ...    

def compare_and_report():
    crm_data = get_crm_data()
    db_data = get_db_data()
    ...
    diff = ...
    result = save_data(diff)
    ...
    send_report(report)

def save_data(data):  # Takes more than a minute
    ...

As you are trying to improve compare_and_report it takes 5 minutes for everytime you run.

A possible solution would be to use devcache, like so:

Decorate appropriate methods

@devcache(group='crm')
def get_crm_data():  # Takes multiple minutes
     ...

@devcache(group='db')
def get_db_data():  # Takes multiple minutes
    ...    

@devcache(group='save')
def save_data(data):  # Takes more than a minute
    ...

Create a config at ~/.devcache/devcache.yaml

props:
    1:
        group: crm
        use_cache: true
    2:
        group: db
        use_cache: true
    3:
        group: save
        use_cache: true

Now the methods will pull data from the cache as use_cache is true. If a change was required to any saved data set the use_cache to false and data will be generated and stored fresh in the cache.

Now using the cache each testing iteration takes seconds instead of minutes.

Other useful configuration

refresh: true  # refresh true will ignore use_cache and refresh all cached data 
enabled: false # will disable everything and will not save new values to cache
props:
    1:
        group: crm
        use_cache: false
    2:
        group: db
        use_cache: true
    3:
        group: save
        use_cache: true
    -1:  # first props match is used.  
         # ordering is by the key (ie -1, 1 ,2 ,3)   
         # 'group' is optional
        pattern: '.*sfdc.*' # matches fully qualified name of the method.  
                            # this pattern would match everything in a module called sfdc
        use_cache: true
        enabled: true # Can turn off props with enabled.  Will allow for other props to potentially match 

Other devcache args

# devcache defaults to not take into account the args
@devcache(group='crm', key_args=('a', ))
def my_method(a, b, c):  # Will cache result using only arg 'a' value as part of the key 
    ...        

@devcache(group='crm', ignore_key_args=('c', ))
def another_method(a, b, c):  # Will cache result using arg 'a', 'b' value as part of the key ignoring 'c' 
    ...        

@devcache(config_file='../alternate.yaml')
def method3(a, b, c):  # specify another configuration 
    ...        

Important Warning

This project is only useful to speed up development and is a security risk.

Best practice would be to not include devcache in project requirements for production and only installing it locally.

Creating project specific decorator will allow for functionality to work in the desired env and not break the other.

For example:

def cacher(config_file=None, group=None, key_args=None, ignore_key_args=None):
    def noop_decorator(func):
        return func  # pass through

    try:
        from devcache import devcache
        return devcache(config_file=config_file, group=group, key_args=key_args, ignore_key_args=ignore_key_args)
    except:
        return noop_decorator

Using @cacher decorator would have use a pass through decorator for prod but use devcache where it's installed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

devcache-1.0.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

devcache-1.0.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file devcache-1.0.2.tar.gz.

File metadata

  • Download URL: devcache-1.0.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.6

File hashes

Hashes for devcache-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f554eedc8d7d4205862c8951611d6ec6635e60bc5d804ec382433c6422b7aeeb
MD5 684393291e624adb4d51b40d3b9ea962
BLAKE2b-256 9b86c83b617d2989157e28bbd43f86063b9f9a1a556c1f71dc8b37bd6a5c00d9

See more details on using hashes here.

File details

Details for the file devcache-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: devcache-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.6

File hashes

Hashes for devcache-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8906d37327a5783163dfe9eb2489d7328a7833b558d1a8a7b6c512754928932a
MD5 a42bce33943ed728b329cf8b0626f09f
BLAKE2b-256 128e3fdb1924b31390867172f26392d6dc2b2d583ffe716e2a4fd7392e6576af

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page