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 
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

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.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

devcache-1.0.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: devcache-1.0.1.tar.gz
  • Upload date:
  • Size: 7.7 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.1.tar.gz
Algorithm Hash digest
SHA256 d19a2c590fad98ee9facfb9ae321ce4bd65135b1d046b27a9adee314e6f39c2c
MD5 54fa9f7105ef833ce8b3c3e9003af919
BLAKE2b-256 58486e6b89b635899908361b4f4750d1c0968dab91261f36296b7673546b0448

See more details on using hashes here.

File details

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

File metadata

  • Download URL: devcache-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eca914c9468705088998fdabffba52ed62b5f613f7e9f26c5fe07d69f7fe5626
MD5 0d89fbf468302304f5e7e33c4b6bf8df
BLAKE2b-256 3306ce2ce935e34fef2576d713b38dca7dc5aed2c5f6b62958ad1c931b008c5a

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