

Hammer.Backend.Redis — hammer\_backend\_redis v6.1.2

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L1 "View Source")

Hammer.Backend.Redis

(hammer\_backend\_redis v6.1.2)

==========================================================================================================================================================================================

Documentation for Hammer.Backend.Redis

This backend uses the [Redix](https://hex.pm/packages/redix) library to connect to Redis.

The backend process is started by calling `start_link`:

```

Hammer.Backend.Redis.start\_link(

 expiry\_ms: 60\_000 \* 10,

 redix\_config: [host: "example.com", port: 5050]

)

```

Options are:

* `expiry_ms`: Expiry time of buckets in milliseconds,

used to set TTL on Redis keys. This configuration is mandatory.

* `redix_config`: Keyword list of options to the [`Redix`](https://hexdocs.pm/redix/1.2.0/Redix.html) redis client,

also aliased to `redis_config`

* `redis_url`: String url of redis server to connect to

(optional, invokes Redix.start\_link/2)

[Link to this section](#summary)

 Summary

==========================================

[Types](#types)

----------------

[bucket\_info()](#t:bucket_info/0)

[bucket\_key()](#t:bucket_key/0)

[Functions](#functions)

------------------------

[child\_spec(init\_arg)](#child_spec/1)

Returns a specification to start this module under a supervisor.

[count\_hit(pid, key, now)](#count_hit/3)

Record a hit in the bucket identified by `key`

[count\_hit(pid, key, now, increment)](#count_hit/4)

Record a hit in the bucket identified by `key`, with a custom increment

[delete\_buckets(pid, id)](#delete_buckets/2)

Delete all buckets associated with `id`.

[get\_bucket(pid, key)](#get_bucket/2)

Retrieve information about the bucket identified by `key`

[start()](#start/0)

[start(args)](#start/1)

[start\_link()](#start_link/0)

[start\_link(args)](#start_link/1)

[stop()](#stop/0)

[Link to this section](#types)

Types

=====================================

[Link to this type](#t:bucket_info/0 "Link to this type")

bucket\_info()

==============

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L29 "View Source")

```

@type bucket_info() ::

  {key :: [bucket\_key](#t:bucket_key/0)(), count :: [integer](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), created :: [integer](https://hexdocs.pm/elixir/typespecs.html#basic-types)(),

   updated :: [integer](https://hexdocs.pm/elixir/typespecs.html#basic-types)()}

```

[Link to this type](#t:bucket_key/0 "Link to this type")

bucket\_key()

=============

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L28 "View Source")

```

@type bucket_key() :: {bucket :: [integer](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), id :: [String.t](https://hexdocs.pm/elixir/String.html#t:t/0)()}

```

[Link to this section](#functions)

Functions

=============================================

[Link to this function](#child_spec/1 "Link to this function")

child\_spec(init\_arg)

======================

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L26 "View Source")

Returns a specification to start this module under a supervisor.

See [`Supervisor`](https://hexdocs.pm/elixir/Supervisor.html).

[Link to this function](#count_hit/3 "Link to this function")

count\_hit(pid, key, now)

=========================

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L62 "View Source")

Record a hit in the bucket identified by `key`

[Link to this function](#count_hit/4 "Link to this function")

count\_hit(pid, key, now, increment)

====================================

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L70 "View Source")

Record a hit in the bucket identified by `key`, with a custom increment

[Link to this function](#delete_buckets/2 "Link to this function")

delete\_buckets(pid, id)

========================

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L86 "View Source")

Delete all buckets associated with `id`.

[Link to this function](#get_bucket/2 "Link to this function")

get\_bucket(pid, key)

=====================

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L78 "View Source")

Retrieve information about the bucket identified by `key`

[Link to this function](#start/0 "Link to this function")

start()

=======

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L34 "View Source")

```

@spec start() :: :ignore | {:error, [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()} | {:ok, [pid](https://hexdocs.pm/elixir/typespecs.html#basic-types)()}

```

[Link to this function](#start/1 "Link to this function")

start(args)

===========

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L39 "View Source")

```

@spec start([keyword](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()) :: :ignore | {:error, [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()} | {:ok, [pid](https://hexdocs.pm/elixir/typespecs.html#basic-types)()}

```

[Link to this function](#start_link/0 "Link to this function")

start\_link()

=============

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L44 "View Source")

```

@spec start_link() :: :ignore | {:error, [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()} | {:ok, [pid](https://hexdocs.pm/elixir/typespecs.html#basic-types)()}

```

[Link to this function](#start_link/1 "Link to this function")

start\_link(args)

=================

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L49 "View Source")

```

@spec start_link([keyword](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()) :: :ignore | {:error, [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()} | {:ok, [pid](https://hexdocs.pm/elixir/typespecs.html#basic-types)()}

```

[Link to this function](#stop/0 "Link to this function")

stop()

======

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/lib/hammer_backend_redis.ex#L54 "View Source")

```

@spec stop() :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```





Overview — hammer\_backend\_redis v6.1.2

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

[View Source](https://github.com/ExHammer/hammer-backend-redis/blob/v6.1.2/guides/Overview.md#L1 "View Source")

Overview

=====================================================================================================================================

A Redis backend for the Hammer rate-limiter

[Hammer](https://github.com/ExHammer/hammer) is a rate-limiter for

the [Elixir](https://elixir-lang.org/) language. It's killer feature is a

pluggable backend system, allowing you to use whichever storage suits your

needs.

This package provides a Redis backend for Hammer, using

the [Redix](https://github.com/whatyouhide/redix) library to connect to the

Redis server.

To get started, read

the [Hammer Tutorial](https://hexdocs.pm/hammer/tutorial.html) first, then add

the `hammer_backend_redis` dependency:

```

 def deps do

 [{:hammer\_backend\_redis, "~> 6.1"},

 {:hammer, "~> 6.0"}]

 end

```

... then configure the `:hammer` application to use the Redis backend:

```

config :hammer,

 backend: {Hammer.Backend.Redis, [expiry\_ms: 60\_000 \* 60 \* 2,

 redix\_config: [host: "some.host"]]}

```

(the `redix_config` arg is a keyword-list which is passed to Redix, it's also

aliased to `redis_config`, with an `s`)

And that's it, calls to [`Hammer.check_rate/3`](https://hexdocs.pm/hammer/6.1.0/Hammer.html#check_rate/3) and so on will use Redis to store

the rate-limit counters.

[← Previous Page

 

API Reference](api-reference.html)

[Next Page →

 

Changelog](changelog.html)

