1"""
2Memcached sdb Module
3
4:maintainer:    SaltStack
5:maturity:      New
6:depends:       python-memcached
7:platform:      all
8
9This module allows access to memcached using an ``sdb://`` URI. This
10package is located at ``https://pypi.python.org/pypi/python-memcached``.
11
12Like all sdb modules, the memcached module requires a configuration profile to
13be configured in either the minion or master configuration file. This profile
14requires very little. In the example:
15
16.. code-block:: yaml
17
18    mymemcache:
19      driver: memcached
20      memcached.host: localhost
21      memcached.port: 11211
22
23The ``driver`` refers to the memcached module, ``host`` and ``port`` the
24memcached server to connect to (defaults to ``localhost`` and ``11211``,
25and ``mymemcached`` refers to the name that will appear in the URI:
26
27.. code-block:: yaml
28
29    password: sdb://mymemcached/mykey
30
31"""
32
33import logging
34
35import salt.utils.memcached
36
37DEFAULT_HOST = "127.0.0.1"
38DEFAULT_PORT = 11211
39DEFAULT_EXPIRATION = 0
40
41log = logging.getLogger(__name__)
42
43__func_alias__ = {"set_": "set"}
44
45
46def __virtual__():
47    """
48    Only load the module if memcached is installed
49    """
50    if salt.utils.memcached.HAS_LIBS:
51        return True
52    return False
53
54
55def set_(key, value, profile=None):
56    """
57    Set a key/value pair in memcached
58    """
59    conn = salt.utils.memcached.get_conn(profile)
60    time = profile.get("expire", DEFAULT_EXPIRATION)
61    return salt.utils.memcached.set_(conn, key, value, time=time)
62
63
64def get(key, profile=None):
65    """
66    Get a value from memcached
67    """
68    conn = salt.utils.memcached.get_conn(profile)
69    return salt.utils.memcached.get(conn, key)
70