Arduino-Redis
A Redis library for Arduino.
Classes | Public Types | Public Member Functions | List of all members
Redis Class Reference

Detailed Description

Redis-for-Arduino client interface.

The sole constructor takes a reference to any instance of a Client subclass, allowing it to be used with any library that abstracts the client bytestream in this way (such as the built-in Ethernet class or the WiFiClient for devices such as the ESP8266).

Definition at line 55 of file Redis.h.

Public Types

typedef void(* RedisMsgCallback) (Redis *, String, String)
 
typedef void(* RedisMsgErrorCallback) (Redis *, RedisMessageError)
 

Public Member Functions

 Redis (Client &client)
 
 Redis (const Redis &)=delete
 
Redisoperator= (const Redis &)=delete
 
 Redis (const Redis &&)=delete
 
Redisoperator= (const Redis &&)=delete
 
RedisReturnValue authenticate (const char *password)
 
bool set (const char *key, const char *value)
 
String get (const char *key)
 
bool del (const char *key)
 
bool exists (const char *key)
 
int append (const char *key, const char *value)
 
int publish (const char *channel, const char *message)
 
bool expire (const char *key, int seconds)
 
bool expire_at (const char *key, int timestamp)
 
bool pexpire (const char *key, int ms)
 
bool pexpire_at (const char *key, int timestamp)
 
bool persist (const char *key)
 
int ttl (const char *key)
 
int pttl (const char *key)
 
bool hset (const char *key, const char *field, const char *value)
 
bool hsetnx (const char *key, const char *field, const char *value)
 
String hget (const char *key, const char *field)
 
bool hdel (const char *key, const char *field)
 
int hlen (const char *key)
 
int hstrlen (const char *key, const char *field)
 
bool hexists (const char *key, const char *field)
 
std::vector< String > lrange (const char *key, int start, int stop)
 
bool subscribe (const char *channel)
 
bool psubscribe (const char *pattern)
 
bool unsubscribe (const char *channelOrPattern)
 
RedisSubscribeResult startSubscribing (RedisMsgCallback messageCallback, RedisMsgErrorCallback errorCallback=nullptr)
 
void stopSubscribing ()
 

#include <Redis.h>

Constructor & Destructor Documentation

◆ Redis()

Redis::Redis ( Client &  client)
inline

Create a Redis connection using Client reference client.

Parameters
clientA Client instance representing the connection to a Redis server.
Returns
An initialized Redis client using client to communicate with the server.

Definition at line 66 of file Redis.h.

Member Function Documentation

◆ append()

int Redis::append ( const char *  key,
const char *  value 
)

Appends value to key.

Parameters
key
value
Returns
The length of the string after the append operation.

Definition at line 39 of file Redis.cpp.

◆ authenticate()

RedisReturnValue Redis::authenticate ( const char *  password)

Authenticate with the given password.

Parameters
passwordThe password with which to authenticate.
Returns
RedisReturnValue detailing the result

Definition at line 4 of file Redis.cpp.

◆ del()

bool Redis::del ( const char *  key)

Delete key.

Parameters
key
Returns
true if key was removed.

Definition at line 34 of file Redis.cpp.

◆ exists()

bool Redis::exists ( const char *  key)

Determine if key exists.

Parameters
key
Returns
true if key exists.

Definition at line 49 of file Redis.cpp.

◆ expire()

bool Redis::expire ( const char *  key,
int  seconds 
)
inline

Expire a key in seconds.

Parameters
keyThe key name for which to set expire time.
secondsThe number of seconds (from "now") at which this key will expire.
Returns
true if the expire time was set successfully, false otherwise.

Definition at line 135 of file Redis.h.

◆ expire_at()

bool Redis::expire_at ( const char *  key,
int  timestamp 
)
inline

Expire a key at UNIX timestamp timestamp (seconds since January 1, 1970).

Note
A timestamp in the past will delete the key immediately.
Parameters
keyThe key name for which to set expire time.
timestampThe UNIX timestamp at which this key will expire.
Returns
true if the expire time was set successfully, false otherwise.

Definition at line 144 of file Redis.h.

◆ get()

String Redis::get ( const char *  key)

Get key.

Parameters
keyThe key name to retrieve.
Returns
The key`s value as a string, null if the key does not exist.

Definition at line 29 of file Redis.cpp.

◆ hdel()

bool Redis::hdel ( const char *  key,
const char *  field 
)

Delete the field stored in hash at key.

Parameters
key
field
Returns
true if deleted

Definition at line 79 of file Redis.cpp.

◆ hexists()

bool Redis::hexists ( const char *  key,
const char *  field 
)

Determine if field exists in hash at key.

Parameters
key
field
Returns
true if key exists.

Definition at line 94 of file Redis.cpp.

◆ hget()

String Redis::hget ( const char *  key,
const char *  field 
)

Gets field stored in hash at key.

Parameters
key
field
Returns
The field`s value.

Definition at line 74 of file Redis.cpp.

◆ hlen()

int Redis::hlen ( const char *  key)

Gets the number of fields stored in hash at key.

Parameters
key
Returns
The number of fields.

Definition at line 84 of file Redis.cpp.

◆ hset()

bool Redis::hset ( const char *  key,
const char *  field,
const char *  value 
)
inline

Set field in hash at key to value.

Parameters
key
field
value
Returns
true if set, false if was updated

Definition at line 193 of file Redis.h.

◆ hsetnx()

bool Redis::hsetnx ( const char *  key,
const char *  field,
const char *  value 
)
inline

Set field in hash at key to value i.f.f. field does not yet exist.

Parameters
key
field
value
Returns
true if set, false if field already existed

Definition at line 202 of file Redis.h.

◆ hstrlen()

int Redis::hstrlen ( const char *  key,
const char *  field 
)

Gets the length of the string at field in hash named key.

Parameters
key
field
Returns
The field's string length value.

Definition at line 89 of file Redis.cpp.

◆ lrange()

std::vector< String > Redis::lrange ( const char *  key,
int  start,
int  stop 
)

Returns the specified elements of the list stored at key.

Parameters
startZero-based starting index (can be negative to indicate end-of-list offset).
endZero-based ending index.
Returns
The list of elements, as a vector of Strings; or an empty vector if error/DNE.

Definition at line 99 of file Redis.cpp.

◆ persist()

bool Redis::persist ( const char *  key)

Persist key (remove any expiry).

Parameters
keyThe key to persist.
Returns
true if the timeout was removed, false if key DNE or had no expiry.

Definition at line 59 of file Redis.cpp.

◆ pexpire()

bool Redis::pexpire ( const char *  key,
int  ms 
)
inline

Expire a key in milliseconds.

Parameters
keyThe key name for which to set expire time.
millisecondsThe number of milliseconds (from "now") at which this key will expire.
Returns
true if the expire time was set successfully, false otherwise.

Definition at line 152 of file Redis.h.

◆ pexpire_at()

bool Redis::pexpire_at ( const char *  key,
int  timestamp 
)
inline

Expire a key at UNIX timestamp timestamp (milliseconds since January 1, 1970).

Note
A timestamp in the past will delete the key immediately.
Parameters
keyThe key name for which to set expire time.
timestampThe UNIX timestamp at which this key will expire.
Returns
true if the expire time was set successfully, false otherwise.

Definition at line 161 of file Redis.h.

◆ psubscribe()

bool Redis::psubscribe ( const char *  pattern)
inline

Sets up a subscription for messages published to any channels matching pattern. May be called in any mode & from message handlers.

Definition at line 258 of file Redis.h.

◆ pttl()

int Redis::pttl ( const char *  key)
inline

Query remaining time-to-live (time-until-expiry) for key.

Parameters
keyThe query whose TTL to query.
Returns
The key`s TTL in milliseconds, or a negative value signaling error: -1 if the key exists but has no associated expire, -2 if the key DNE.

Definition at line 184 of file Redis.h.

◆ publish()

int Redis::publish ( const char *  channel,
const char *  message 
)

Publish message to channel.

Parameters
channelThe channel on which to publish the message.
messageThe message to be published to the channel.
Returns
The number of subscribers to the published message.

Definition at line 44 of file Redis.cpp.

◆ set()

bool Redis::set ( const char *  key,
const char *  value 
)

Set key to value.

Note
Current implementation only supports basic SET without behavioral modification options added in Redis 2.6.12. To expire a set key, use the expire() method below.
Parameters
keyThe key name to set
valueThe value to set for key
Returns
true if key was set to value, false if error.

Definition at line 22 of file Redis.cpp.

◆ startSubscribing()

RedisSubscribeResult Redis::startSubscribing ( RedisMsgCallback  messageCallback,
RedisMsgErrorCallback  errorCallback = nullptr 
)

Enters subscription mode and subscribes to all channels/patterns setup via subscribe()/psubscribe(). On success, this call will block until stopSubscribing() is called (meaning loop() will never be called!), and only then will return RedisSubscribeSuccess. On failure, this call will return immediately with a return value indicated the failure mode. Calling stopSubscribing() will force this method to exit on the next recieved message.

Parameters
messageCallbackThe function to be called on each successful message receipt.
errorCallbackThe function to be called if message receipt processing produces an error. Call stopSubscribing() on the passed-in instance to end all further message processing.

Definition at line 131 of file Redis.cpp.

◆ stopSubscribing()

void Redis::stopSubscribing ( )
inline

Stops message processing on receipt of next message. Can be called from message handlers.

Definition at line 279 of file Redis.h.

◆ subscribe()

bool Redis::subscribe ( const char *  channel)
inline

Sets up a subscription for messages published to channel. May be called in any mode & from message handlers.

Definition at line 253 of file Redis.h.

◆ ttl()

int Redis::ttl ( const char *  key)
inline

Query remaining time-to-live (time-until-expiry) for key.

Parameters
keyThe query whose TTL to query.
Returns
The key's TTL in seconds, or a negative value signaling error: -1 if the key exists but has no associated expire, -2 if the key DNE.

Definition at line 176 of file Redis.h.

◆ unsubscribe()

bool Redis::unsubscribe ( const char *  channelOrPattern)

Removes a subscription for channelOrPattern. May be called from message handlers.

Definition at line 119 of file Redis.cpp.