iredis
Interactive Redis: A Terminal Client for Redis with AutoCompletion and Syntax Highlighting.
Top Related Projects
asyncio (PEP 3156) Redis support
A better Python REPL
Quick Overview
iredis
is a user-friendly, feature-rich, and interactive Redis client that provides a more intuitive and efficient way to interact with Redis databases. It offers a range of advanced features and functionalities to enhance the Redis development experience.
Pros
- Intuitive Interface:
iredis
provides a clean and user-friendly interface, making it easier for developers to navigate and interact with Redis databases. - Advanced Features: The client supports a wide range of Redis commands, including support for Lua scripting, transaction management, and key-value management.
- Productivity Enhancements:
iredis
includes features like auto-completion, syntax highlighting, and command history, which can significantly improve developer productivity. - Cross-Platform Compatibility: The client is compatible with various operating systems, including Windows, macOS, and Linux, making it accessible to a wide range of users.
Cons
- Limited Offline Functionality: While
iredis
is a powerful tool for interacting with Redis databases, it may not be suitable for offline or standalone use cases, as it requires an active connection to a Redis server. - Potential Learning Curve: Users who are new to Redis or command-line interfaces may need to invest some time in learning the tool's features and commands to fully utilize its capabilities.
- Dependency on Redis: As
iredis
is designed specifically for Redis, it may not be suitable for users who need to interact with other types of databases or data stores. - Potential Performance Overhead: Depending on the size and complexity of the Redis database, the additional features and functionality provided by
iredis
may introduce some performance overhead compared to using the standard Redis command-line interface.
Code Examples
# Connect to a Redis server
from iredis import Redis
r = Redis(host='localhost', port=6379)
# Set a key-value pair
r.set('mykey', 'myvalue')
# Get the value of a key
value = r.get('mykey')
print(value) # Output: b'myvalue'
# Execute a Redis command
result = r.execute_command('PING')
print(result) # Output: b'PONG'
This code demonstrates how to use the iredis
library to connect to a Redis server, set and retrieve key-value pairs, and execute Redis commands.
# Use Lua scripting
script = """
local value = redis.call('GET', KEYS[1])
if value then
return value
else
return 'Key not found'
end
"""
result = r.eval(script, 1, 'mykey')
print(result) # Output: b'myvalue' or b'Key not found'
This code shows how to use the iredis
library to execute a Lua script on the Redis server, which retrieves the value of a specified key.
# Manage transactions
r.multi()
r.set('key1', 'value1')
r.set('key2', 'value2')
result = r.execute()
print(result) # Output: [True, True]
This code demonstrates how to use the iredis
library to manage Redis transactions, including the ability to batch multiple commands and execute them atomically.
Getting Started
To get started with iredis
, follow these steps:
- Install the
iredis
package using pip:
pip install iredis
- Launch the
iredis
client:
iredis
- Once the client is running, you can start interacting with your Redis database. Some basic commands include:
set key value
: Set the value of a keyget key
: Retrieve the value of a keykeys *
: List all keys in the databaseping
: Check the connection to the Redis serverhelp
: Display the available commands and their usage
- For more advanced usage, you can explore the various features and functionalities provided by
iredis
, such as Lua scripting, transaction management, and key-value management.
Competitor Comparisons
asyncio (PEP 3156) Redis support
Pros of aioredis-py
- Supports a wide range of Redis commands and features, including Pub/Sub, Transactions, and Scripting.
- Provides a high-level API for interacting with Redis, making it easier to write asynchronous code.
- Actively maintained and has a larger community compared to iredis.
Cons of aioredis-py
- Requires more boilerplate code to set up and configure the Redis connection.
- May have a steeper learning curve for developers who are new to asynchronous programming.
- Lacks some of the advanced features and customization options available in iredis.
Code Comparison
aioredis-py:
import asyncio
import aioredis
async def main():
redis = await aioredis.create_redis_pool('redis://localhost')
await redis.set('key', 'value')
value = await redis.get('key')
print(value)
redis.close()
await redis.wait_closed()
asyncio.run(main())
iredis:
from iredis import Redis
r = Redis()
r.set('key', 'value')
value = r.get('key')
print(value)
A better Python REPL
Pros of ptpython
- Supports a wide range of features, including syntax highlighting, code completion, and key bindings.
- Provides a more feature-rich and customizable REPL experience compared to the standard Python REPL.
- Integrates well with other Python libraries, such as IPython, for a more powerful interactive environment.
Cons of ptpython
- May have a steeper learning curve compared to the simpler iredis.
- Can be more resource-intensive, especially for users with older or less powerful hardware.
- May not be as focused on a specific use case (like Redis) as iredis.
Code Comparison
iredis (laixintao/iredis):
import iredis
r = iredis.Redis()
r.set('foo', 'bar')
print(r.get('foo')) # Output: b'bar'
ptpython (prompt-toolkit/ptpython):
from ptpython.repl import embed
embed(globals(), locals(), vi_mode=True)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Interactive Redis: A Cli for Redis with AutoCompletion and Syntax Highlighting.
IRedis is a terminal client for redis with auto-completion and syntax highlighting. IRedis lets you type Redis commands smoothly, and displays results in a user-friendly format.
IRedis is an alternative for redis-cli. In most cases, IRedis behaves exactly
the same as redis-cli. Besides, it is safer to use IRedis on production servers
than redis-cli: IRedis will prevent accidentally running dangerous commands,
like KEYS *
(see
Redis docs / Latency generated by slow commands).
Features
- Advanced code completion. If you run command
KEYS
then runDEL
, IRedis will auto-complete your command based onKEYS
result. - Command validation. IRedis will validate command while you are typing, and
highlight errors. E.g. try
CLUSTER MEET IP PORT
, IRedis will validate IP and PORT for you. - Command highlighting, fully based on redis grammar. Any valid command in IRedis shell is a valid redis command.
- Human-friendly result display.
- pipeline feature, you can use your favorite shell tools to parse redis'
response, like
get json | jq .
. - Support pager for long output.
- Support connection via URL,
iredis --url redis://example.com:6379/1
. - Support cluster, IRedis will auto reissue command for
MOVED
response in cluster mode. - Store server configuration:
iredis -d prod-redis
(see dsn for more). peek
command to check the key's type then automatically callget
/lrange
/sscan
, etc, depending on types. You don't need to call thetype
command then type another command to get the value.peek
will also display the key's length and memory usage.- Ctrl + C to cancel the current typed command, this won't exit IRedis, exactly like bash behaviour. Use Ctrl + D to send a EOF to exit IRedis.
- Ctrl + R to open reverse-i-search to search through your command history.
- Auto suggestions. (Like fish shell.)
- Support
--encode=utf-8
, to decode Redis' bytes responses. - Command hint on bottom, include command syntax, supported redis version, and time complexity.
- Official docs with built-in
HELP
command, tryHELP SET
! - Written in pure Python, but IRedis was packaged into a single binary with PyOxidizer, you can use cURL to download and run, it just works, even you don't have a Python interpreter.
- You can change the cli prompt using
--prompt
option or set via~/.iredisrc
config file. - Hide password for
AUTH
command. - Says "Goodbye!" to you when you exit!
- For full features, please see: iredis.xbin.io
Install
Pip
Install via pip:
pip install iredis
pipx is recommended:
pipx install iredis
Brew
For Mac users, you can install iredis via brew ð»
brew install iredis
Linux
You can also use your Linux package manager to install IRedis, like apt
in
Ubuntu (Only available on Ubuntu 21.04+).
apt install iredis
Download Binary
Or you can download the executable binary with cURL(or wget), untar, then run. It is especially useful when you don't have a python interpreter(E.g. the official Redis docker image which doesn't have Python installed.):
wget https://github.com/laixintao/iredis/releases/latest/download/iredis.tar.gz \
&& tar -xzf iredis.tar.gz \
&& ./iredis
(Check the release page if you want to download an old version of IRedis.)
Usage
Once you install IRedis, you will know how to use it. Just remember, IRedis
supports similar options like redis-cli, like -h
for redis-server's host and
-p
for port.
$ iredis --help
Usage: iredis [OPTIONS] [CMD]...
IRedis: Interactive Redis
When no command is given, IRedis starts in interactive mode.
Examples:
- iredis
- iredis -d dsn
- iredis -h 127.0.0.1 -p 6379
- iredis -h 127.0.0.1 -p 6379 -a <password>
- iredis --url redis://localhost:7890/3
Type "help" in interactive mode for information on available commands and
settings.
Options:
-h TEXT Server hostname (default: 127.0.0.1).
-p TEXT Server port (default: 6379).
-s, --socket TEXT Server socket (overrides hostname and port).
-n INTEGER Database number.(overwrites dsn/url's db
number)
-u, --username TEXT User name used to auth, will be ignore for
redis version < 6.
-a, --password TEXT Password to use when connecting to the
server.
--url TEXT Use Redis URL to indicate connection(Can set
with env `IREDIS_URL`), Example: redis:/
/[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6
379/0 unix://[[username]:[password]]@/pa
th/to/socket.sock?db=0
-d, --dsn TEXT Use DSN configured into the [alias_dsn]
section of iredisrc file. (Can set with env
`IREDIS_DSN`)
--newbie / --no-newbie Show command hints and useful helps.
--iredisrc TEXT Config file for iredis, default is
~/.iredisrc.
--decode TEXT decode response, default is No decode, which
will output all bytes literals.
--client_name TEXT Assign a name to the current connection.
--raw / --no-raw Use raw formatting for replies (default when
STDOUT is not a tty). However, you can use
--no-raw to force formatted output even when
STDOUT is not a tty.
--rainbow / --no-rainbow Display colorful prompt.
--shell / --no-shell Allow to run shell commands, default to
True.
--pager / --no-pager Using pager when output is too tall for your
window, default to True.
--verify-ssl [none|optional|required]
Set the TLS certificate verification
strategy
--prompt TEXT Prompt format (supported interpolations:
{client_name}, {db}, {host}, {path}, {port},
{username}, {client_addr}, {client_id}).
--version Show the version and exit.
--help Show this message and exit.
Using DSN
IRedis support storing server configuration in config file. Here is a DSN config:
[alias_dsn]
dev=redis://localhost:6379/4
staging=redis://username:password@staging-redis.example.com:6379/1
Put this in your iredisrc
then connect via iredis -d staging
or
iredis -d dev
.
Change The Default Prompt
You can change the prompt str, the default prompt is:
127.0.0.1:6379>
Which is rendered by {host}:{port}[{db}]>
, you can change this via --prompt
option or change
iredisrc
config file. The prompwt string uses python string format engine, supported
interpolations:
{client_name}
{db}
{host}
{path}
{port}
{username}
{client_addr}
{client_id}
The --prompt
utilize
Python String format engine,
so as long as it is a valid string formatter, it will work( anything that
"<your prompt>".format(...)
accepts). For example, you can limit your Redis
server host name's length to 5 by setting --prompt
to
iredis --prompt '{host:.5s}'
.
Configuration
IRedis supports config files. Command-line options will always take precedence over config. Configuration resolution from highest to lowest precedence is:
- Options from command line
$PWD/.iredisrc
~/.iredisrc
(this path can be changed withiredis --iredisrc $YOUR_PATH
)/etc/iredisrc
- default config in IRedis package.
You can copy the self-explained default config here:
https://raw.githubusercontent.com/laixintao/iredis/master/iredis/data/iredisrc
And then make your own changes.
(If you are using an old versions of IRedis, please use the config file below, and change the version in URL):
https://raw.githubusercontent.com/laixintao/iredis/v1.0.4/iredis/data/iredisrc
Keys
IRedis support unix/readline-style REPL keyboard shortcuts, which means keys like Ctrl + F to forward work.
Also:
- Ctrl + D (i.e. EOF) to exit; you can also use the
exit
command. - Ctrl + L to clear screen; you can also use the
clear
command. - Ctrl + X Ctrl + E to open an editor to edit command, or V in vi-mode.
Development
Release Strategy
IRedis is built and released by GitHub Actions
. Whenever a tag is pushed to
the master
branch, a new release is built and uploaded to pypi.org, it's very
convenient.
Thus, we release as often as possible, so that users can always enjoy the new features and bugfixes quickly. Any bugfix or new feature will get at least a patch release, whereas big features will get a minor release.
Setup Environment
IRedis favors poetry as package management tool. To setup a develop environment on your computer:
First, install poetry (you can do it in a python's virtualenv):
pip install poetry
Then run (which is similar to pip install -e .
):
poetry install
Be careful running testcases locally, it may flush you db!!!
Development Logs
This is a command-line tool, so we don't write logs to stdout.
You can tail -f ~/.iredis.log
to see logs, the log is pretty clear, you can
see what actually happens from log files.
Catch Up with Latest Redis-doc
IRedis use a git submodule to track current-up-to-date redis-doc version. To catch up with latest:
- Git pull in redis-doc
- Copy doc files to
/data
:cp -r redis-doc/commands* iredis/data
- Prettier
markdown
prettier --prose-wrap always iredis/data/commands/*.md --write
- Check the diff, update IRedis' code if needed.
Related Projects
If you like iredis, you may also like other cli tools by dbcli:
- pgcli - Postgres Client with Auto-completion and Syntax Highlighting
- mycli - MySQL/MariaDB/Percona Client with Auto-completion and Syntax Highlighting
- litecli - SQLite Client with Auto-completion and Syntax Highlighting
- mssql-cli - Microsoft SQL Server Client with Auto-completion and Syntax Highlighting
- athenacli - AWS Athena Client with Auto-completion and Syntax Highlighting
- vcli - VerticaDB client
- iredis - Client for Redis with AutoCompletion and Syntax Highlighting
IRedis is build on the top of prompt_toolkit, a Python library (by Jonathan Slenders) for building rich commandline applications.
Top Related Projects
asyncio (PEP 3156) Redis support
A better Python REPL
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot