Skip to content

Commit

Permalink
feat: Added memcached service (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
secobarbital authored Sep 8, 2024
1 parent e0a19db commit c196208
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 5 deletions.
9 changes: 9 additions & 0 deletions doc/memcached.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Memcached

[memcached](https://www.memcached.org/) is a high-performance, distributed memory object caching system, generic in nature, but originally intended for use in speeding up dynamic web applications by alleviating database load.

You can think of it as a short-term memory for your applications.

## Usage example

<https://github.com/juspay/services-flake/blob/main/nix/services/memcached_test.nix>
11 changes: 6 additions & 5 deletions doc/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ short-title: Services
# Supported services

- [[apache-kafka]]#
- [[cassandra]]#
- [[clickhouse]]#
- [[elasticsearch]]#
- [[grafana]]#
- [[tempo]]
- [[memcached]]#
- [[mysql]]#
- [[nginx]]#
- [[ollama]]#
- [[open-webui]]#
- [[postgresql]]#
- [[pgadmin]]
- [[prometheus]]#
- [[redis]]#
- [[redis-cluster]]
- [[searxng]]#
- [[tika]]#
- [[zookeeper]]#
- [[grafana]]#
- [[tempo]]
- [[prometheus]]#
- [[cassandra]]#
- [[weaviate]]#
- [[zookeeper]]#
1 change: 1 addition & 0 deletions nix/services/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ in
./redis.nix
./zookeeper.nix
./grafana.nix
./memcached.nix
./prometheus.nix
./pgadmin.nix
./cassandra.nix
Expand Down
62 changes: 62 additions & 0 deletions nix/services/memcached.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Based on https://github.com/cachix/devenv/blob/main/src/modules/services/memcached.nix
{ pkgs, lib, name, config, ... }:
let
inherit (lib) types;
in
{
options = {
package = lib.mkPackageOption pkgs "memcached" { };

bind = lib.mkOption {
type = types.nullOr types.str;
default = "127.0.0.1";
description = ''
The IP interface to bind to.
`null` means "all interfaces".
'';
example = "127.0.0.1";
};

port = lib.mkOption {
type = types.port;
default = 11211;
description = ''
The TCP port to accept connections.
If port 0 is specified memcached will not listen on a TCP socket.
'';
};

startArgs = lib.mkOption {
type = types.listOf types.lines;
default = [ ];
example = [ "--memory-limit=100M" ];
description = ''
Additional arguments passed to `memcached` during startup.
'';
};
};

config = {
outputs.settings.processes.${name} = {
command = "${config.package}/bin/memcached --port=${toString config.port} --listen=${config.bind} ${lib.concatStringsSep " " config.startArgs}";

readiness_probe = {
exec.command = ''
echo -e "stats\nquit" | ${pkgs.netcat}/bin/nc ${config.bind} ${toString config.port} > /dev/null 2>&1
'';
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;
success_threshold = 1;
failure_threshold = 5;
};

# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy
availability = {
restart = "on_failure";
max_restarts = 5;
};
};
};
}
23 changes: 23 additions & 0 deletions nix/services/memcached_test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{ pkgs, config, ... }:
{
services.memcached."memcached1".enable = true;

settings.processes.test =
let
cfg = config.services.memcached."memcached1";
in
{
command = pkgs.writeShellApplication {
runtimeInputs = [
cfg.package
pkgs.gnugrep
pkgs.netcat
];
text = ''
echo -e "stats\nquit" | nc 127.0.0.1 11211 | grep "STAT version"
'';
name = "memcached-test";
};
depends_on."memcached1".condition = "process_healthy";
};
}
1 change: 1 addition & 0 deletions test/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"${inputs.services-flake}/nix/services/tempo_test.nix"
"${inputs.services-flake}/nix/services/weaviate_test.nix"
"${inputs.services-flake}/nix/services/tika_test.nix"
"${inputs.services-flake}/nix/services/memcached_test.nix"
]));
};
};
Expand Down

0 comments on commit c196208

Please sign in to comment.