Giter Club home page Giter Club logo

Comments (5)

jordanfbrown avatar jordanfbrown commented on May 26, 2024 1

We noticed this as well and ended up subclassing the Rollout class to add request-level memoization via the RequestStore gem. There's a probably a better way to do it but this worked well enough for us.

class MemoizedRollout < Rollout
  def active?(feature, user = nil)
    key = key_for(feature, user)
    RequestStore.fetch(key) { super }
  end

  def activate(feature)
    clear_cache(feature)
    super
  end

  def deactivate(feature)
    clear_cache(feature)
    super
  end

  def activate_user(feature, user)
    clear_cache(feature)
    super
  end

  def deactivate_user(feature, user)
    clear_cache(feature)
    super
  end

  private

  def clear_cache(feature)
    RequestStore.store.keys.each do |key|
      next unless key.to_s.start_with?("rollout:#{feature}")
      RequestStore.delete(key)
    end
  end

  def key_for(feature, user)
    ['rollout', feature.to_s, user&.class&.to_s, user&.id].compact.join(':')
  end
end

from rollout.

berniechiu avatar berniechiu commented on May 26, 2024

Cool! @jordanfbrown, thanks for sharing!

from rollout.

jnunemaker avatar jnunemaker commented on May 26, 2024

Rollout isn’t overly public about it but I believe it uses the adapter pattern by only using like two of Redis methods. You can inject anything you like which means you can make a class with same interface that wraps the Redis client with memorization.

from rollout.

jnunemaker avatar jnunemaker commented on May 26, 2024

Kind what @jordanfbrown did but you shouldn’t need to subclass rollout. You can just define the same interface and initialize with a Redis instance. Your class could be MemoizedRedis. Then you just pass that into Rollout.new. The only other issue with memoization is to turn it on per request or job and ensure it is cleared after. You can do this with middleware or in before action calls in rails. Middleware early in the stack is best.

from rollout.

berniechiu avatar berniechiu commented on May 26, 2024

@jnunemaker Ha thanks!

I was just thinking about how https://github.com/jnunemaker/flipper works when opened the issue here

from rollout.

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.