CACHEDB_MONGODB module
Admin Guide
Section titled “Admin Guide”Overview
Section titled “Overview”This module is an implementation of a cache system designed to work with MongoDB servers. It implements the Key-Value interface exposed by the OpenSIPS core.
The underlying client library is compatible with any of the following MongoDB server versions: 2.4, 2.6, 3.0, 3.2 and 3.4, as stated in the MongoDB documentation.
Advantages
Section titled “Advantages”- memory costs are no longer on the server
- many servers can be used inside a cluster, so the memory is virtually unlimited
- the cache is 100% persistent. A restart of OpenSIPS server will not affect the DB. The MongoDB is also persistent so it can also be restarted without loss of information.
- MongoDB is an open-source project so it can be used to exchange data with various other applications
- By creating a MongoDB Cluster, multiple OpenSIPS instances can easily share key-value information
- This module also implements the CacheDB Raw query capability, thus you can run whatever query that the MongoDB back-end supports, taking full advatange of it.
Limitations
Section titled “Limitations”- keys (in key:value pairs) may not contain spaces or control characters
Dependencies
Section titled “Dependencies”OpenSIPS Modules
Section titled “OpenSIPS Modules”None.
External Libraries or Applications
Section titled “External Libraries or Applications”The following packages must be installed before running OpenSIPS with this module loaded:
# Debian / Ubuntusudo apt-get install libjson-c2 libmongoc-1.0
# Red Hat / CentOSsudo yum install json-c mongo-c-driverThe following packages are required in order to compile this module:
# Debian / Ubuntusudo apt-get install libjson-c-dev libmongoc-dev libbson-dev
# Red Hat / CentOSsudo yum install json-c-devel mongo-c-driver-develExported Parameters
Section titled “Exported Parameters”cachedb_url (string)
Section titled “cachedb_url (string)”The URLs of the server groups that OpenSIPS will connect to in order to allow the cache_store(), cache_fetch(), etc. functions to be used from the OpenSIPS script. It can be set more than one time. The prefix part of the URL will be the identifier that will be used from the script.
The URL syntax is identical to the one used by MongoDB, including connect string options. For more info, please refer to the official MongoDB connect string documentation.
...# Connect to a single mongod instancemodparam("cachedb_mongodb", "cachedb_url", "mongodb://localhost:27017/opensipsDB.dialog")
# Connect to a mongod replica setmodparam("cachedb_mongodb", "cachedb_url", "mongodb://10.0.0.10,10.0.0.11:27017/opensipsDB.dialog?replicaSet=my-set")
# Connect to a mongos instance (routes to a sharded cluster)modparam("cachedb_mongodb", "cachedb_url", "mongodb://localhost/opensipsDB.dialog")
# Example of multiple connections:# * to a main mongos, with failover to a backup mongos# * to a single mongodmodparam("cachedb_mongodb", "cachedb_url", "mongodb:cluster://localhost,10.0.0.10:27017/opensipsDB.dialog")modparam("cachedb_mongodb", "cachedb_url", "mongodb://localhost:27017/opensipsDB.userlocation")......cache_store("mongodb", "key", "$ru value");cache_remove("mongodb:cluster", "key");cache_fetch("mongodb:instance1", "key", $avp(10));...exec_threshold (int)
Section titled “exec_threshold (int)”The maximum number of microseconds that a mongodb query can last. Anything above the threshold will trigger a warning message to the log
Default value is “0 ( unlimited - no warnings )“.
...modparam("cachedb_mongodb", "exec_threshold", 100000)...compat_mode_2.4 (int)
Section titled “compat_mode_2.4 (int)”Switch the module into compatibility mode for MongoDB 2.4 servers. Specifically, this allows “insert/update/delete” raw queries to not fail, since they were introduced in MongoDB 2.6. The module will interpret the raw query JSON, convert it to its corresponding command and run it.
Caveat: only the minimally required raw query options are supported in this mode.
Default value is “0 (disabled)“.
...modparam("cachedb_mongodb", "compat_mode_2.4", 1)...compat_mode_3.0 (int)
Section titled “compat_mode_3.0 (int)”Switch the module into compatibility mode for MongoDB 2.6/3.0 servers. Specifically, this allows “find” raw queries to not fail, since they were introduced in MongoDB 3.2. The module will interpret the “find” raw query JSON, convert it to its corresponding command and run it.
Caveat: only the minimally required options for “find” raw queries are supported in this mode.
Default value is “0 (disabled)”.
...modparam("cachedb_mongodb", "compat_mode_3.0", 1)...Exported Functions
Section titled “Exported Functions”The module does not export functions to be used in configuration script.
Raw Query Syntax
Section titled “Raw Query Syntax”The cachedb_mongodb module supports raw queries, thus taking full advantage of the capabilities of the back-end, including query-specific options such as read/write preference, timeouts, filtering options, etc.
The query syntax is identical to the mongo cli. Documentation for it can be found on the MongoDB website. Query results are returned as JSON documents, that one can further process in the OpenSIPS script by using the JSON module.
Some example raw queries:
...cache_raw_query("mongodb:cluster", "{ \ \"insert\": \"ip_blacklist\", \ \"documents\": [{ \ \"username\": \"$fU\", \ \"ip\": \"$si\", \ \"attempts\": 1 \ }]}", "$avp(out)");xlog("INSERT RAW QUERY returned $rc, output: '$avp(out)'\n");......cache_raw_query("mongodb:cluster", "{ \ \"update\": \"ip_blacklist\", \ \"updates\": [{ \ \"q\": { \ \"username\": \"$fU\", \ \"ip\": \"$si\" \ }, \ \"u\": { \ \"$$inc\": {\"attempts\": 1} \ } \ }]}", "$avp(out)");xlog("UPDATE RAW QUERY returned $rc, output: '$avp(out)'\n");...Contributors
Section titled “Contributors”By Commit Statistics
Section titled “By Commit Statistics”Top contributors by DevScore(1), authored commits(2) and lines added/removed(3)
| # | Name | DevScore | Commits | Lines++ | Lines— |
|---|---|---|---|---|---|
| 1. | Liviu Chircu (@liviuchircu) | 153 | 78 | 3073 | 2920 |
| 2. | Vlad Paiu (@vladpaiu) | 35 | 9 | 2965 | 51 |
| 3. | Razvan Crainea (@razvancrainea) | 14 | 12 | 32 | 29 |
| 4. | Ovidiu Sas (@ovidiusas) | 10 | 8 | 94 | 18 |
| 5. | Stefan Darius (@dariusstefan) | 8 | 4 | 314 | 46 |
| 6. | @jalung | 6 | 1 | 195 | 154 |
| 7. | Vlad Patrascu (@rvlad-patrascu) | 5 | 3 | 87 | 2 |
| 8. | Bogdan-Andrei Iancu (@bogdan-iancu) | 5 | 3 | 3 | 5 |
| 9. | Dan Pascu (@danpascu) | 4 | 2 | 4 | 4 |
| 10. | Peter Lemenkov (@lemenkov) | 3 | 1 | 1 | 1 |
All remaining contributors: Julián Moreno Patiño, Alessio Garzi (@Ozzyboshi).
(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
(3) ignoring whitespace edits, renamed files and auto-generated files
By Commit Activity
Section titled “By Commit Activity”| # | Name | Commit Activity |
|---|---|---|
| 1. | Stefan Darius (@dariusstefan) | Jun 2026 - Jul 2026 |
| 2. | Razvan Crainea (@razvancrainea) | Aug 2015 - Jun 2026 |
| 3. | Bogdan-Andrei Iancu (@bogdan-iancu) | Oct 2014 - Jun 2020 |
| 4. | Vlad Patrascu (@rvlad-patrascu) | May 2017 - Jun 2020 |
| 5. | Liviu Chircu (@liviuchircu) | Mar 2014 - Feb 2020 |
| 6. | Alessio Garzi (@Ozzyboshi) | Nov 2019 - Nov 2019 |
| 7. | Dan Pascu (@danpascu) | May 2019 - May 2019 |
| 8. | Peter Lemenkov (@lemenkov) | Jun 2018 - Jun 2018 |
| 9. | @jalung | Aug 2017 - Aug 2017 |
| 10. | Julián Moreno Patiño | Feb 2016 - Feb 2016 |
All remaining contributors: Vlad Paiu (@vladpaiu), Ovidiu Sas (@ovidiusas).
(1) including any documentation-related commits, excluding merge commits
Documentation
Section titled “Documentation”Contributors
Section titled “Contributors”Last edited by: Razvan Crainea (@razvancrainea), Liviu Chircu (@liviuchircu), Peter Lemenkov (@lemenkov), Julián Moreno Patiño, Vlad Paiu (@vladpaiu).
License
Section titled “License”All documentation files (i.e. .md extension) are licensed under the Creative Common License 4.0