crawlee-python icon indicating copy to clipboard operation
crawlee-python copied to clipboard

Better approach of making a cache

Open vdusek opened this issue 1 year ago • 6 comments

  • This is a follow-up issue to the discussion in https://github.com/apify/crawlee-py/pull/82#discussion_r1548009445.
  • Currently, we have our own implementation of LRU cache in crawlee/_utils/lru_cache.py.
  • Let's do it in a more Pythonic way, maybe utilizing the built-in caching from functools std module (lru_cache decorator)?

vdusek avatar Apr 03 '24 15:04 vdusek

Was reading on this and wanted to clear up somethings. The lru_cache from functools has no delete function for a given key, it just clears whole cache. Can you define what do you mean by pythonic? The cache implementation looks good as it.

Edit: I just saw the usage of this class and del wasn't used atleast in the python codebase. Please do tell me if it is also being used in the ts base. I will try to implement functools LRU . Will update you if there is any progress from my side.

Azathoth-X avatar Oct 04 '24 15:10 Azathoth-X

i want to work on this issue ,kindly assign me @B4nan , if it's still opened

29deepanshutyagi avatar Oct 05 '24 05:10 29deepanshutyagi

i want to work on this issue ,kindly assign me @B4nan , if it's still opened

We don't assign issues for hacktoberfest. If you want to work on this, open a PR. First mergeable one gets merged.

janbuchar avatar Oct 06 '24 10:10 janbuchar

Can you define what do you mean by pythonic?

Try using something from the standard libraries.

vdusek avatar Oct 08 '24 17:10 vdusek

Hi @vdusek , Just wanted to ask do we need the delete individual type saved in cache function, in this class. I could only trace references of get, clear and creating key value pair for LRUcache class. Only place where the delete function is used is unit tests.

def test_del(lru_cache: LRUCache[int]) -> None:
    # Key error on non-existent key
    with pytest.raises(KeyError):
        del lru_cache['non-existent-key']
    # No error with existing key
    len_before_del = len(lru_cache)
    del lru_cache['a']
    assert len(lru_cache) == len_before_del - 1
    assert 'a' not in lru_cache

So. if using stl, is the delete function to be kept in the implementation or not?

Azathoth-X avatar Oct 08 '24 19:10 Azathoth-X

Hi @Azathoth-X, if the delete is used only in the tests, I believe we do not need it.

vdusek avatar Oct 10 '24 10:10 vdusek