Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jun 27 04:59
    aletor123 synchronize #430
  • Jun 27 04:50
    aletor123 synchronize #430
  • Jun 27 02:19
    aletor123 synchronize #430
  • Jun 27 02:03
    aletor123 commented #430
  • Jun 27 02:02
    aletor123 synchronize #430
  • Jun 27 02:01
    aletor123 synchronize #430
  • Jun 22 08:15
    Suor commented #430
  • Jun 21 13:54
    aletor123 commented #430
  • Jun 10 11:45
    hamidrabedi opened #432
  • Jun 03 09:01
    aletor123 commented #430
  • Jun 03 09:00
    aletor123 commented #430
  • Jun 03 08:49
    aletor123 synchronize #430
  • Jun 03 03:41
    Suor commented #430
  • Jun 02 12:58
    aletor123 opened #431
  • Jun 02 02:01
    aletor123 edited #430
  • Jun 02 02:00
    aletor123 synchronize #430
  • Jun 02 01:59
    aletor123 opened #430
  • May 27 05:55
    Suor commented #428
  • May 27 05:24

    Suor on 6.1

    (compare)

  • May 27 05:24

    Suor on master

    Up to 6.1 (compare)

Alexander Schepanovski
@Suor
Maybe you can post the code?
Chetan Dhembre
@chetandhembre
def get_a(start, end):
    start_str = start.date().isoformat()
    end_str = end.date().isoformat()
    key = '%s-%s' % (start_str, end_str)

    @cached_as(extra=key, timeout=30 * 60)
    def _get_a():
        a = some_function_call()
        # do not want to cache results if a are empty
        if len(a.keys()) == 0:
            e = UncachedResult(a)
            raise e

        return a

    try:
        return _get_a()
    except UncachedResult, e:
        return e.result
Alexander Schepanovski
@Suor
First argument to @cached_as() should be queryset or model or object
This is why it's called "cached as"
And I don't see invalidation here
Chetan Dhembre
@chetandhembre
can i use cached?
Alexander Schepanovski
@Suor
Anyway, if you want to use invalidation you need to use simple - @cached(), and unnest _get_a() moving extra to arg
Chetan Dhembre
@chetandhembre
problem with that is my current nested function will get (key, start, end) as argument
start/end are datetime with second level granularity
how can i just use only single argument for caching purpose?
Alexander Schepanovski
@Suor
Just use start and end, and don't bother with key
@cached(timeout=30 * 60)
def _get_a(start, end):
    a = some_function_call()
    # Do cache empty results
    if not a:
        raise UncachedResult(a)

    return a

def get_a(start, end):
    start_str = start.date().isoformat()
    end_str = end.date().isoformat()

    try:
        return _get_a(start, end)
    except UncachedResult as e:
        return e.result
Chetan Dhembre
@chetandhembre
actually i missed one thing in my code here. .i am using start and end in my some_function_call() which are mostly get from datetime.utcnow()
so above your solution wont work
i need second level granularity for start and end
Alexander Schepanovski
@Suor
It will, just pass then
*them
What do you mean by second level granularity?
So it's
@cached(timeout=30 * 60)
def _get_a(start, end):
    a = some_function_call(start, end)
    # Do cache empty results
    if not a:
        raise UncachedResult(a)

    return a

def get_a(start, end):
    try:
        return _get_a(start, end)
    except UncachedResult as e:
        return e.result
Chetan Dhembre
@chetandhembre
end = datetime.now()
start = end - timedelta(days-12)
what will get key of cache result for _get_a()?
Alexander Schepanovski
@Suor
To get a key:
_get_a.key(start, end)
To invalidate:
_get_a.invalidate(start, end)
Chetan Dhembre
@chetandhembre
i want to cache result for 30 minutes ..
so result for datetime.datetime(2016, 10, 14, 12, 17, 0, 0) will be same for datetime.datetime(2016, 10, 14, 12, 3, 0, 0)
Alexander Schepanovski
@Suor
Then do not pass timestamp to _get_a() and some_func(), pass dates.
Chetan Dhembre
@chetandhembre
i can not
Alexander Schepanovski
@Suor
why?
Your function doesn't depend on time, right?
Chetan Dhembre
@chetandhembre
it depend on time .. but i want to cache result on interval of time .. so result i will get 12:00 PM same result i will return for next 30 minutes
Alexander Schepanovski
@Suor
When you need to round your times to 30 min intervals then use that as arguments.
Chetan Dhembre
@chetandhembre
problem with that is if i invalidate data on 12:15 due to some bug or issue.. and then again cache result it will calcaulate result for 12:00 as i am sending things in 30 min interval
i hope i am able to explain my usecase
Alexander Schepanovski
@Suor
Should there be single result at each given time?
Chetan Dhembre
@chetandhembre
yeah
Alexander Schepanovski
@Suor
Then you should have single cache key not depending on anything
So cache a function without arguments and calc dates within it
Chetan Dhembre
@chetandhembre
it is not also possible my function gets start and end from another function .. i guess cacheops wont help me with my unique usecase
atleast function level cache
will use from cacheops import cache in this case
Alexander Schepanovski
@Suor
You can use custom key func here
Chetan Dhembre
@chetandhembre
like?
does cacheops provide this feature? it is in TODOS
Alexander Schepanovski
@Suor
def _key_func(func, args, kwargs, extra=None):
    start, end = args
    # ...
    return key

@cached_as(timeout=30 * 60, key_func=_key_func)
def _get_a(start, end):
    a = some_function_call(start, end)
    # Do not cache empty results
    if not a:
        raise UncachedResult(a)

    return a
Chetan Dhembre
@chetandhembre
this is great ..
Alexander Schepanovski
@Suor
I feel that you are overcomplicating though.
Chetan Dhembre
@chetandhembre
why i havent read it before
Alexander Schepanovski
@Suor
Because it's not documented
Chetan Dhembre
@chetandhembre
ok
Chetan Dhembre
@chetandhembre
btw thanks for cacheops
Alexander Schepanovski
@Suor
you are welcome