Skip to content

Commit 38a9286

Browse files
committed
More documentation.
1 parent 7d68499 commit 38a9286

File tree

3 files changed

+112
-99
lines changed

3 files changed

+112
-99
lines changed

guides/getting-started/readme.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Getting Started
2+
3+
This guide explains how to use the `async-redis` gem to connect to a Redis server and perform basic operations.
4+
5+
## Installation
6+
7+
Add the gem to your project:
8+
9+
``` shell
10+
$ bundle add async-redis
11+
```
12+
13+
## Usage
14+
15+
### Basic Local Connection
16+
17+
``` ruby
18+
require 'async/redis'
19+
20+
endpoint = Async::Redis.local_endpoint
21+
client = Async::Redis::Client.new(endpoint)
22+
23+
Async do
24+
puts client.info
25+
ensure
26+
client.close
27+
end
28+
```
29+
30+
### Connecting to Redis SSL Endpoint
31+
32+
This example demonstrates parsing an environment variable with a `redis://` or SSL `rediss://` scheme, and demonstrates how you can specify SSL parameters on the SSLContext object.
33+
34+
``` ruby
35+
require 'async/redis'
36+
37+
def make_redis_endpoint(uri)
38+
tcp_endpoint = ::IO::Endpoint.tcp(uri.hostname, uri.port)
39+
case uri.scheme
40+
when 'redis'
41+
tcp_endpoint
42+
when 'rediss'
43+
ssl_context = OpenSSL::SSL::SSLContext.new
44+
ssl_context.set_params(
45+
ca_file: "/path/to/ca.crt",
46+
cert: OpenSSL::X509::Certificate.new(File.read("client.crt")),
47+
key: OpenSSL::PKey::RSA.new(File.read("client.key")),
48+
)
49+
::IO::SSLEndpoint.new(tcp_endpoint, ssl_context: ssl_context)
50+
else
51+
raise ArgumentError
52+
end
53+
end
54+
55+
endpoint = make_redis_endpoint(URI(ENV['REDIS_URL']))
56+
client = Async::Redis::Client.new(endpoint)
57+
58+
# ...
59+
```
60+
61+
### Variables
62+
63+
``` ruby
64+
require 'async'
65+
require 'async/redis'
66+
67+
endpoint = Async::Redis.local_endpoint
68+
client = Async::Redis::Client.new(endpoint)
69+
70+
Async do
71+
client.set('X', 10)
72+
puts client.get('X')
73+
ensure
74+
client.close
75+
end
76+
```
77+
78+
### Subscriptions
79+
80+
``` ruby
81+
require 'async'
82+
require 'async/redis'
83+
84+
endpoint = Async::Redis.local_endpoint
85+
client = Async::Redis::Client.new(endpoint)
86+
87+
Async do |task|
88+
condition = Async::Condition.new
89+
90+
publisher = task.async do
91+
condition.wait
92+
93+
client.publish 'status.frontend', 'good'
94+
end
95+
96+
subscriber = task.async do
97+
client.subscribe 'status.frontend' do |context|
98+
condition.signal # We are waiting for messages.
99+
100+
type, name, message = context.listen
101+
102+
pp type, name, message
103+
end
104+
end
105+
ensure
106+
client.close
107+
end
108+
```

guides/links.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
getting-started:
2+
order: 1

readme.md

+2-99
Original file line numberDiff line numberDiff line change
@@ -4,108 +4,11 @@ An asynchronous client for Redis including TLS. Support for streaming requests a
44

55
[![Development Status](https://github.com/socketry/async-redis/workflows/Test/badge.svg)](https://github.com/socketry/async-redis/actions?workflow=Test)
66

7-
## Installation
8-
9-
``` shell
10-
$ bundle add async-redis
11-
```
12-
137
## Usage
148

15-
### Basic Local Connection
16-
17-
``` ruby
18-
require 'async/redis'
19-
20-
endpoint = Async::Redis.local_endpoint
21-
client = Async::Redis::Client.new(endpoint)
22-
23-
Async do
24-
pp client.info
25-
ensure
26-
client.close
27-
end
28-
```
29-
30-
### Connecting to Redis SSL Endpoint
31-
32-
This example demonstrates parsing an environment variable with a `redis://` or SSL `rediss://` scheme, and demonstrates how you can specify SSL parameters on the SSLContext object.
33-
34-
``` ruby
35-
require 'async/redis'
36-
37-
def make_redis_endpoint(uri)
38-
tcp_endpoint = ::IO::Endpoint.tcp(uri.hostname, uri.port)
39-
case uri.scheme
40-
when 'redis'
41-
tcp_endpoint
42-
when 'rediss'
43-
ssl_context = OpenSSL::SSL::SSLContext.new
44-
ssl_context.set_params(
45-
ca_file: "/path/to/ca.crt",
46-
cert: OpenSSL::X509::Certificate.new(File.read("client.crt")),
47-
key: OpenSSL::PKey::RSA.new(File.read("client.key")),
48-
)
49-
::IO::SSLEndpoint.new(tcp_endpoint, ssl_context: ssl_context)
50-
else
51-
raise ArgumentError
52-
end
53-
end
54-
55-
endpoint = make_redis_endpoint(URI(ENV['REDIS_URL']))
56-
client = Async::Redis::Client.new(endpoint)
57-
58-
# ...
59-
```
60-
61-
### Variables
62-
63-
``` ruby
64-
require 'async'
65-
require 'async/redis'
66-
67-
endpoint = Async::Redis.local_endpoint
68-
client = Async::Redis::Client.new(endpoint)
69-
70-
Async do
71-
client.set('X', 10)
72-
pp client.get('X')
73-
ensure
74-
client.close
75-
end
76-
```
77-
78-
### Subscriptions
79-
80-
``` ruby
81-
require 'async'
82-
require 'async/redis'
83-
84-
endpoint = Async::Redis.local_endpoint
85-
client = Async::Redis::Client.new(endpoint)
9+
Please see the [project documentation](https://github.com/socketry/async-redis) for more details.
8610

87-
Async do |task|
88-
condition = Async::Condition.new
89-
90-
publisher = task.async do
91-
condition.wait
92-
93-
client.publish 'status.frontend', 'good'
94-
end
95-
96-
subscriber = task.async do
97-
client.subscribe 'status.frontend' do |context|
98-
condition.signal # We are waiting for messages.
99-
100-
type, name, message = context.listen
101-
102-
pp type, name, message
103-
end
104-
end
105-
ensure
106-
client.close
107-
end
108-
```
11+
- [Getting Started](https://github.com/socketry/async-redisguides/getting-started/index) - This guide explains how to use the `async-redis` gem to connect to a Redis server and perform basic operations.
10912

11013
## Contributing
11114

0 commit comments

Comments
 (0)