<commit_before>vmod_redis
==========

A Varnish module that allows sending commands to redis from the VCL.

This extension is mostly a proof-of-concept; it has only received minimal testing. However, I do run this in production for a heavy traffic website, and except for the expected slowdown when calling Redis, I've had no other issues.

At the very minimum, it will slow down Varnish a fair amount (at least a few milliseconds per request, depending on how fast your network and your redis server are).

So far the module builds and runs on FreeBSD and Linux (tested on CentOS and Ubuntu).

Usage
-----

In your VCL file, include the following line to load the redis module.

```
import redis;
```

Functions
---------

### redis.init_redis

```
redis.init_redis(host, port, timeout_ms)
```

Use the redis server at the given `host` and `port` with a timeout of `timeout_ms` milliseconds. If `port` is less than or equal to zero, the default port of 6379 is used. If `timeout_ms` is less than or equal to zero, a default timeout of `200ms` is used.

This function is intended to be called from the Varnish subroutine `vcl_init`. If the call is left out, the module will attempt to connect to the Redis server at `127.0.0.1:6379` with a connect timeout of `200ms`.

**Example:**

```
redis.init_redis("localhost", 6379, 200);
```

### redis.send

```
redis.send(command)
```

Sends the given `command` to redis; the response will be ignored.

**Example:**

```
redis.send("LPUSH client " + client.ip);
```

### redis.call

```
redis.call(command)
```

Sends the given `command` to redis; any response will be returned as a string.

**Example:**

```
set req.http.x-redis = redis.call("LTRIM client 0 99");
```

Examples
--------

See the [`examples/`](/examples) directory.

Dependencies
------------

* [hiredis](https://github.com/redis/hiredis) - minimalistic C client for Redis
* automake
* autoconf

Installation
------------

#### Step 1. Download Varnish Source

On Ubuntu, this can be easily done using `apt-get source varnish`. However, you can get the source for Varnish on any OS using Git.

```
cd /opt
git clone https://github.com/varnish/Varnish-Cache.git varnish
cd varnish
git checkout tags/varnish-3.0.3
```

**Important:** You must download the same version of the Varnish source code as you have installed, or you'll get an error when loading the vmod. You can get your current version using `varnishd -V`.

#### Step 2. Compile & Install

```
./autogen.sh
./configure VARNISHSRC=/opt/varnish VMODDIR=/usr/lib/varnish/vmods
make
sudo make install
```

Be sure to point `VARNISHSRC` to wherever you downloaded the Varnish source code to.

**Important:** Some systems such as CentOS 64-bit store vmods in `/usr/lib64/varnish/vmods` not `/usr/lib/varnish/vmods`, so change that if needed.

Troubleshooting
---------------

#### Could not load module redis

If you get an error like this when starting Varnish:

```
Message from VCC-compiler:
Could not load module redis
/usr/lib64/varnish/vmods/libvmod_redis.so
ABI mismatch, expected <Varnish 3.0.4 9f83e8f>, got <Varnish 3.0.5 1a89b1f>
('input' Line 7 Pos 8)
```

You compiled `libvmod-redis` against the wrong version of the Varnish source code.
<commit_msg>Add missing dependencies to the list<commit_after>vmod_redis
==========

A Varnish module that allows sending commands to redis from the VCL.

This extension is mostly a proof-of-concept; it has only received minimal testing. However, I do run this in production for a heavy traffic website, and except for the expected slowdown when calling Redis, I've had no other issues.

At the very minimum, it will slow down Varnish a fair amount (at least a few milliseconds per request, depending on how fast your network and your redis server are).

So far the module builds and runs on FreeBSD and Linux (tested on CentOS and Ubuntu).

Usage
-----

In your VCL file, include the following line to load the redis module.

```
import redis;
```

Functions
---------

### redis.init_redis

```
redis.init_redis(host, port, timeout_ms)
```

Use the redis server at the given `host` and `port` with a timeout of `timeout_ms` milliseconds. If `port` is less than or equal to zero, the default port of 6379 is used. If `timeout_ms` is less than or equal to zero, a default timeout of `200ms` is used.

This function is intended to be called from the Varnish subroutine `vcl_init`. If the call is left out, the module will attempt to connect to the Redis server at `127.0.0.1:6379` with a connect timeout of `200ms`.

**Example:**

```
redis.init_redis("localhost", 6379, 200);
```

### redis.send

```
redis.send(command)
```

Sends the given `command` to redis; the response will be ignored.

**Example:**

```
redis.send("LPUSH client " + client.ip);
```

### redis.call

```
redis.call(command)
```

Sends the given `command` to redis; any response will be returned as a string.

**Example:**

```
set req.http.x-redis = redis.call("LTRIM client 0 99");
```

Examples
--------

See the [`examples/`](/examples) directory.

Dependencies
------------

* [hiredis](https://github.com/redis/hiredis) - minimalistic C client for Redis (`libhiredis-dev` on Ubuntu)
* automake
* autoconf
* libtool

Installation
------------

#### Step 1. Download Varnish Source

On Ubuntu, this can be easily done using `apt-get source varnish`. However, you can get the source for Varnish on any OS using Git.

```
cd /opt
git clone https://github.com/varnish/Varnish-Cache.git varnish
cd varnish
git checkout tags/varnish-3.0.3
```

**Important:** You must download the same version of the Varnish source code as you have installed, or you'll get an error when loading the vmod. You can get your current version using `varnishd -V`.

#### Step 2. Compile & Install

```
./autogen.sh
./configure VARNISHSRC=/opt/varnish VMODDIR=/usr/lib/varnish/vmods
make
sudo make install
```

Be sure to point `VARNISHSRC` to wherever you downloaded the Varnish source code to.

**Important:** Some systems such as CentOS 64-bit store vmods in `/usr/lib64/varnish/vmods` not `/usr/lib/varnish/vmods`, so change that if needed.

Troubleshooting
---------------

#### Could not load module redis

If you get an error like this when starting Varnish:

```
Message from VCC-compiler:
Could not load module redis
/usr/lib64/varnish/vmods/libvmod_redis.so
ABI mismatch, expected <Varnish 3.0.4 9f83e8f>, got <Varnish 3.0.5 1a89b1f>
('input' Line 7 Pos 8)
```

You compiled `libvmod-redis` against the wrong version of the Varnish source code.

{'fraction_non_alphanumeric': 0.12051517939282429, 'fraction_numerical': 0.017479300827966882, 'mean_word_length': 3.8247041420118344, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5743570', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2050, 'n_words': 871}