Once and only once Try it

Have you ever wanted to only do something once? Perhaps you've been trying to send an activation event to MixPanel, or perhaps you're trying to ensure that you only ever send an emails one time, whatever the case you probably felt the need to create a new table, build a model for it and store a teeny boolean to mark that you've done the thing. Annoying right?

But of course that's only half the battle. To actually use it you'd need something that reads that table, then depending on your use case you might also want to wrap some caching in there so things don't get slow. Wouldn't it be neat if all this were easy?

Well now it is easy.

client = Prefab::Client.new
ratelimit_client = client.ratelimit_client(timeout: 30)

# only need to do this on startup
ratelimit_client.upsert("send_email", :INFINITE, 1)

# only pass the check once per user
if ratelimit_client.pass?("send_email:welcome_email:user12312")

Say you're interested in only sending the first experiment expsosure event to MixPanel:

client = Prefab::Client.new
ratelimit_client = client.ratelimit_client(timeout: 30)
ratelimit_client.upsert("events:experiment", :INFINITE, 1) # only send things with the same key once

def track_event(event_props)
   key_parts = ['events',

   key = key_parts.join(":")
   if ratelimit_client.pass?(key)
Try it