Commit graph

7 commits

Author SHA1 Message Date
Ankush Menat
81b37cb7d2
refactor: clean up code to py310 supported features (#17367)
refactor: clean up code to py39+ supported syntax

- f-strings instead of format
- latest typing support instead of pre 3.9 TitleCase
- remove UTF-8 declarations.
- many more changes

Powered by https://github.com/asottile/pyupgrade/ + manual cleanups
2022-07-01 11:51:05 +05:30
Gavin D'souza
2e5784629b fix: Avoid possible iteration failures passing default in pop 2022-06-09 12:31:05 +05:30
Gavin D'souza
f03a7162ed feat(site_cache): Add maxsize to site_cache, defaults to None 2022-06-07 18:35:46 +05:30
Gavin D'souza
b04e1908f1 perf: Speed up request_cache
Sped up request_cache access times multi-fold with the help of a
benchmarking script. Access times for this generic cache is comparable
to specific caches written (eg: get_meta's local cache) by an additional
overhead of 15% as compared to implementing it in each function separately

* Got rid of logging
* Optimized bits with the help of benchmarking script against
  frappe.get_meta's performance
* Use hash instead of json.dumps
2022-06-07 15:56:26 +05:30
Gavin D'souza
061703ab17 feat(site_cache): Allow 'time to live' param for cache
Want to automatically expire your cache every 60s? site_cache has got you.
2022-06-07 15:56:26 +05:30
gavin
3be3b83c4f feat: frappe.utils.caching.site_cache
Decorator to cache method calls across requests. The cache is stored in
frappe.utils.caching._SITE_CACHE. The cache persists on the parent process.
It offers a light-weight cache for the current process without the additional
overhead of serializing / deserializing Python objects.

Note: This cache isn't shared among workers. If you need to share data across
workers, use redis (frappe.cache API) instead.

Usage:
    from frappe.utils.caching import site_cache

    @site_cache
    def calculate_pi():
        import math, time
        precision = get_precision("Math Constant", "Pi") # depends on
        site data
	return round(math.pi, precision)

    calculate_pi(10) # will calculate value
    calculate_pi(10) # will return value from cache
    calculate_pi.clear_cache() # clear this function's cache for all sites
    calculate_pi(10) # will calculate value
2022-06-07 15:56:26 +05:30
gavin
db9b0d3d4f feat: frappe.utils.caching.request_cache
Utility method for boundless cache. This method maintains it's cache
using Werkzeug local. Therefore, it's invalidated at the end ofthe
lifecycle of a request in our WSGI app.

Cache keys generated are a function of func.__name__, func.__module__,
passed args & kwargs. Key generation will be successful only if args and
kwargs can be safely `json.dumps`'ed.

Usage:

    from frappe.utils.caching import request_cache

    @request_cache
    def calculate_pi(num_terms=0):
        import math, time
        print(f"{num_terms = }")
        time.sleep(10)
        return math.pi

    calculate_pi(10) # will calculate value
    calculate_pi(10) # will return value from cache
2022-06-07 15:56:26 +05:30