GithubHelp home page GithubHelp logo

Comments (3)

whitfin avatar whitfin commented on June 27, 2024

@xward hi! Sorry for the slow response, busy month.

I took a look at your code and there's not much you can do about the incr call themselves, however you can speed it up a little bit by re-using a static state:

Cachex.execute(@store, fn cache ->
    Enum.reduce(checkers, %{}, fn {id, key, period, allowed, decision_if_above}, acc ->
        {:ok, n} = Cachex.incr(cache, key)

        # new ? set ttl
        if n == 1, do: Cachex.expire(cache, key, period)

        Map.put(acc, (n > allowed && decision_if_above) || :pass, id)
    end)
end)

This will skip an ETS table lookup on each call to your cache, which actually is most of the overhead involved here. If you want to try this out, you should see a fair amount of speed up - it might be enough for your use case.

Let me know! Happy to discuss further if you need further improvements, maybe we can figure out something in the API that might help.

from cachex.

xward avatar xward commented on June 27, 2024

It indeed improved performances, thanks ! ~25% with 500 checkers (cf benchmarks)

I guess there is no way around calling incr/2 for each checkers I have.

from cachex.

whitfin avatar whitfin commented on June 27, 2024

@xward there's a potential that something this might also be a little faster.

I haven't been able to test this, not on my work machine - but hopefully you get the idea. Let me know if it helps!

    Cachex.execute(@store, fn cache ->
      Enum.reduce(checkers, %{}, fn {id, key, period, allowed, decision_if_above}, acc ->
        Cachex.get_and_update(cache, key, fn
          (nil) -> { :commit, 1, ttl: period }
          (val) -> { :commit, val + 1 }
        end)
        Map.put(acc, (n > allowed && decision_if_above) || :pass, id)
      end)
    end)

The idea is that it combines it all into one single cache call, rather than multiple - although you still need to call per key.

from cachex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.