SQL Cacher Module

Table of Contents

1. Admin Guide
1.1. Overview
1.2. Dependencies
1.3. Exported Parameters
1.3.1. cache_table (string)
1.3.2. spec_delimiter (string)
1.3.3. pvar_delimiter (string)
1.3.4. columns_delimiter (string)
1.3.5. sql_fetch_nr_rows (integer)
1.3.6. full_caching_expire (integer)
1.3.7. reload_interval (integer)
1.4. Exported Functions
1.5. MI Commands
1.5.1. sql_cacher_reload
1.6. Exported pseudo-variables
1.6.1. $sql_cached_value(id{sep}col{sep}key)
1.7. Usage Example
2. Contributors
2.1. By Commit Statistics
2.2. By Commit Activity
3. Documentation
3.1. Contributors

List of Tables

2.1. Top contributors by DevScore(1), authored commits(2) and lines added/removed(3)
2.2. Most recently active contributors(1) to this module

List of Examples

1.1. cache_table parameter usage
1.2. spec_delimiter parameter usage
1.3. pvar_delimiter parameter usage
1.4. columns_delimiter parameter usage
1.5. sql_fetch_nr_rows parameter usage
1.6. full_caching_expire parameter usage
1.7. reload_interval parameter usage
1.8. sql_cacher_reload usage
1.9. sql_cached_value(id{sep}col{sep}key) pseudo-variable usage
1.10. Example database content - carrierfailureroute table
1.11. Setting the cache_table parameter
1.12. Accessing cached values

Chapter1.Admin Guide


The sql_cacher module introduces the possibility to cache data from a SQL-based database (using different OpenSIPS modules which implement the DB API) into a cache system implemented in OpenSIPS through the CacheDB Interface. This is done by specifying the databases URLs, SQL table to be used, desired columns to be cached and other details in the OpenSIPS configuration script.

The cached data is available in the script through the read-only pseudovariable $sql_cached_value similar to a Key-Value system. A specified column from the SQL table has the role of key therefore the value of this column along with the name of a required column are provided as "parameters" to the pseudovariable returning the appropriate value of the column.

There are two types of caching available:

  • full caching - the entire SQL table (all the rows) is loaded into the cache at OpenSIPS startup;

  • on demand - the rows of the SQL table are loaded at runtime when appropriate keys are requested.

For on demand caching, the stored values have a configurable expire period after which they are permanently removed unless an MI reload function is called for a specific key. In the case of full caching the data is automatically reloaded at a configurable interval. Consequently if the data in the SQL database changes and a MI reload function is called, the old data remains in cache only until it expires.


The following modules must be loaded before this module:

  • The OpenSIPS modules that offer actual database back-end connection

1.3.Exported Parameters

1.3.1.cache_table (string)

This parameter can be set multiple times in order to cache multiple SQL tables or even the same table but with a different configuration. The module distinguishes those different entries by an id string.

The caching entry is specified via this parameter that has it's own subparameters. Each of those parameters are separated by a delimiter configured by spec_delimiter and have the following format:


The parameters are:

  • id : cache entry id

  • db_url : the URL of the SQL database

  • cachedb_url : the URL of the CacheDB database

  • table : SQL database table name

  • key : SQL database column name of the key column

  • columns : names of the columns to be cached from the SQL database, separated by a delimiter configured by columns_delimiter.

    If not present, all the columns from the table will be cached

  • on_demand : specifies the type of caching:

    • 0 : full caching

    • 1 : on demand

    If not present, default value is 0

  • expire : expire period for the values stored in the cache for the on demand caching type in seconds

    If not present, default value is 1 hour

The parameters must be given in the exact order specified above.

Overall, the parameter does not have a default value, it must be set at least once in order to cache any table.

Example1.1.cache_table parameter usage

modparam("sql_cacher", "cache_table",
columns=column_name_1 column_name_2 column_name_3

1.3.2.spec_delimiter (string)

The delimiter to be used in the caching entry specification provided in the cache_table parameter to separate the subparameters. It must be a single character.

The default value is newline.

Example1.2.spec_delimiter parameter usage

modparam("sql_cacher", "spec_delimiter", "\n")

1.3.3.pvar_delimiter (string)

The delimiter to be used in the $sql_cached_value pseudovariable to separate the caching id, the desired column name and the value of the key. It must be a single character.

The default value is :.

Example1.3.pvar_delimiter parameter usage

modparam("sql_cacher", "pvar_delimiter", " ")

1.3.4.columns_delimiter (string)

The delimiter to be used in the columns subparameter of the caching entry specification provided in the cache_table parameter to separate the desired columns names. It must be a single character.

The default value is (space).

Example1.4.columns_delimiter parameter usage

modparam("sql_cacher", "columns_delimiter", ",")

1.3.5.sql_fetch_nr_rows (integer)

The number of rows to be fetched into OpenSIPS private memory in one chunk from the SQL database driver. When querying large tables, adjust this parameter accordingly to avoid the filling of OpenSIPS private memory.

The default value is 100.

Example1.5.sql_fetch_nr_rows parameter usage

modparam("sql_cacher", "sql_fetch_nr_rows", 1000)

1.3.6.full_caching_expire (integer)

Expire period for the values stored in cache for the full caching type in seconds. This is the longest time that deleted or modified data remains in cache.

The default value is 24 hours.

Example1.6.full_caching_expire parameter usage

modparam("sql_cacher", "full_caching_expire", 3600)

1.3.7.reload_interval (integer)

This parameter represents how many seconds before the data expires (for full caching) the automatic reloading is triggered.

The default value is 60 s.

Example1.7.reload_interval parameter usage

modparam("sql_cacher", "reload_interval", 5)

1.4.Exported Functions

No function exported to be used from configuration file.

1.5.MI Commands


Reloads the entire SQL table in cache for full caching or the specified key for on demand caching.

The first parameter is the caching id.

The second parameter must be a value of the key column from the SQL table. For full caching this parameter is not needed.

Example1.8.sql_cacher_reload usage

$ opensipsctl fifo sql_cacher_reload caching_name
$ opensipsctl fifo sql_cacher_reload caching_name key

1.6.Exported pseudo-variables


The cached data is available through this read-only PV.The format is the following:

  • sep : separator configured by pvar_delimiter

  • id : cache entry id

  • col : name of the required column

  • key : value of the key column

Example1.9.sql_cached_value(id{sep}col{sep}key) pseudo-variable usage

$avp(a) = $sql_cached_value(caching_name:column_name_1:key1);

1.7.Usage Example

This section provides an usage example for the caching of an SQL table.

Suppose one in interested in caching the columns: host_name, reply_code, flags and next_domain from the carrierfailureroute table of the OpenSIPS database.

Example1.10.Example database content - carrierfailureroute table

| id | domain  | host_name | reply_code | flags | mask | next_domain |
|  1 |      99 |           | 408        |    16 |   16 |             |
|  2 |      99 | gw1       | 404        |     0 |    0 | 100         |
|  3 |      99 | gw2       | 50.        |     0 |    0 | 100         |
|  4 |      99 |           | 404        |  2048 | 2112 | asterisk-1  |

In the first place, the details of the caching must be provided by setting the module parameter cache_table in the OpenSIPS configuration script.

Example1.11.Setting the cache_table parameter

modparam("sql_cacher", "cache_table",
columns=host_name reply_code flags next_domain")

Next, the values of the cached columns ca be accessed through the $sql_cached_value PV.

Example1.12.Accessing cached values

$avp(rc1) = $sql_cached_value(carrier_fr_caching:reply_code:1);
$avp(rc2) = $sql_cached_value(carrier_fr_caching:reply_code:2);
$avp(nd) = $sql_cached_value(carrier_fr_caching:next_domain:$var(some_id));
xlog("host name is: $sql_cached_value(carrier_fr_caching:host_name:2)");


2.1.By Commit Statistics

Table2.1.Top contributors by DevScore(1), authored commits(2) and lines added/removed(3)

NameDevScoreCommitsLines ++Lines --
1. Vlad Patrascu (@rvlad-patrascu)63233116792
2. Liviu Chircu (@liviuchircu)866446
3. Razvan Crainea (@razvancrainea)86148
4. Bogdan-Andrei Iancu (@bogdan-iancu)4244
5. Ionel Cerghit (@ionel-cerghit)415092
6. Ovidiu Sas (@ovidiusas)3111

(1) DevScore = author_commits + author_lines_added / (project_lines_added / project_commits) + author_lines_deleted / (project_lines_deleted / project_commits)

(2) including any documentation-related commits, excluding merge commits. Regarding imported patches/code, we do our best to count the work on behalf of the proper owner, as per the "fix_authors" and "mod_renames" arrays in opensips/doc/build-contrib.sh. If you identify any patches/commits which do not get properly attributed to you, please submit a pull request which extends "fix_authors" and/or "mod_renames".

(3) ignoring whitespace edits, renamed files and auto-generated files

2.2.By Commit Activity

Table2.2.Most recently active contributors(1) to this module

NameCommit Activity
1. Liviu Chircu (@liviuchircu)Mar 2016 - Nov 2018
2. Vlad Patrascu (@rvlad-patrascu)Aug 2015 - Nov 2018
3. Bogdan-Andrei Iancu (@bogdan-iancu)May 2017 - Jun 2018
4. Razvan Crainea (@razvancrainea)Feb 2016 - Apr 2017
5. Ovidiu Sas (@ovidiusas)Mar 2017 - Mar 2017
6. Ionel Cerghit (@ionel-cerghit)Dec 2015 - Dec 2015

(1) including any documentation-related commits, excluding merge commits



Last edited by: Liviu Chircu (@liviuchircu), Vlad Patrascu (@rvlad-patrascu), Bogdan-Andrei Iancu (@bogdan-iancu).

doc copyrights:

Copyright 2015 www.opensips-solutions.com