Commit graph

19 commits

Author SHA1 Message Date
Gavin D'souza
c6580b5880
refactor: Replace pytz to std lib zoneinfo & datetime
Signed-off-by: Gavin D'souza <gavin.dsouza@switchup.de>
2024-12-06 15:43:33 +05:30
Abdeali Chharchhoda
e9bbe03354 chore: Improve caching decorators docstring 2024-11-10 12:37:46 +05:30
Nikhil Kothari
fb2753fcaf
fix: pass user and shared params when checking for cache keys (#26402)
* fix: pass user and shared params when checking for cache keys

* chore(test): added test for user cache in redis_cache
2024-05-10 12:48:43 +00:00
Ankush Menat
c2d5ef175c
fix: cache github release data per bench (#26382)
Reduce API calls to github as public calls are limited.
2024-05-09 06:52:39 +00:00
Akhil Narang
3f1e19de85
refactor(treewide): enable RUF rules
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-02-21 16:20:28 +05:30
Akhil Narang
94365a59bf
chore(utils/caching): drop unnecessary indent
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-02-19 14:28:35 +05:30
Ankush Menat
de9ac89748 style: re-format with ruff 2024-02-05 18:53:33 +05:30
Ankush Menat
f526054ae2
refactor: Remove usage of utcnow (#23369) 2023-11-23 13:21:27 +05:30
Ankush Menat
adf30693a9 ci: update pyupgrade 2023-07-14 14:24:08 +05:30
Ankush Menat
fa6dc03cc8
refactor: frappe.cache() usage to frappe.cache (#21282) 2023-06-08 11:47:17 +05:30
Ankush Menat
a7413fe4a7 chore: separate out func call key for @redis_cache 2023-06-04 15:13:39 +05:30
Saqib Ansari
32dbbb47bf
feat: redis cache decorator (#20452)
* feat: redis cache decorator

* fix: review changes

* fix: remove unintentional changes

* fix: remove unintentional changes

* refactor: cleanup and simplify code for redis

AIs suck

* fix: bug

* test: redis cache

* fix: remove unused import

* feat: make redis cache user specific

redis cache utils already support this, extending so everyone can use it

* feat: support @redis_cache without params

* test: flake in request site cache test

---------

Co-authored-by: Ankush Menat <ankush@frappe.io>
2023-03-27 17:03:20 +05:30
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