Skip to content

Commit 9ce3743

Browse files
DOC-5503 hide GitHub icon when example is local
1 parent bc372f6 commit 9ce3743

File tree

7 files changed

+380
-5
lines changed

7 files changed

+380
-5
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: "Test Local Examples"
3+
description: "Testing the local code examples feature"
4+
---
5+
6+
# Test Local Examples
7+
8+
This page demonstrates the new local code examples feature that works just like remote examples.
9+
10+
## String Operations
11+
12+
Here's a local example showing string operations:
13+
14+
{{< clients-example local_string_demo incr >}}
15+
> SET mykey "Hello Redis!"
16+
OK
17+
> GET mykey
18+
"Hello Redis!"
19+
> SET counter 10
20+
OK
21+
> INCR counter
22+
(integer) 11
23+
> INCRBY counter 5
24+
(integer) 16
25+
{{< /clients-example >}}
26+
27+
## With Step Highlighting
28+
29+
You can highlight specific steps:
30+
31+
{{< clients-example local_string_demo set_get >}}
32+
> SET mykey "Hello Redis!"
33+
OK
34+
> GET mykey
35+
"Hello Redis!"
36+
{{< /clients-example >}}
37+
38+
## Hash Operations
39+
40+
Here's a local example for hash operations:
41+
42+
{{< clients-example local_hash_demo >}}
43+
> HSET user:1000 name "John Smith"
44+
(integer) 1
45+
> HSET user:1000 email "[email protected]" age 30
46+
(integer) 2
47+
> HGET user:1000 name
48+
"John Smith"
49+
> HGETALL user:1000
50+
1) "name"
51+
2) "John Smith"
52+
3) "email"
53+
54+
5) "age"
55+
6) "30"
56+
{{< /clients-example >}}
57+
58+
## List Operations (Go only)
59+
60+
This example only has a Go implementation:
61+
62+
{{< clients-example local_list_demo >}}
63+
> LPUSH mylist "world"
64+
(integer) 1
65+
> LPUSH mylist "hello"
66+
(integer) 2
67+
> LRANGE mylist 0 -1
68+
1) "hello"
69+
2) "world"
70+
{{< /clients-example >}}
71+
72+
## Language Filtering
73+
74+
Show only Python examples:
75+
76+
{{< clients-example local_hash_demo hset_hget Python >}}
77+
> SET counter 10
78+
OK
79+
> INCR counter
80+
(integer) 11
81+
{{< /clients-example >}}
82+
83+
## Custom Tab Name and Footer Link
84+
85+
Test custom tab name and footer link (no language filtering so footer shows):
86+
87+
{{< clients-example local_string_demo "" "" "" "REST API" "LangCache API" "/develop/ai/langcache/api-reference" >}}
88+
> SET counter 10
89+
OK
90+
> INCR counter
91+
(integer) 11
92+
{{< /clients-example >}}

layouts/partials/tabs/wrapper.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,14 @@
101101
</a>
102102
<div class="w-1/2"></div>
103103
<div class="flex-1 text-right">
104-
<a href='{{ index $tab "sourceUrl" }}' tabindex="1" class="button text-neutral-400 hover:text-slate-100 h-6 w-6 p-1" title="Improve this code example">
105-
<svg width="18" height="16" viewBox="0 0 18 16" fill="github" xmlns="http://www.w3.org/2000/svg">
106-
<path fill-rule="evenodd" clip-rule="evenodd" d="M8.99953 1.43397e-06C7.09918 -0.000897921 5.26058 0.674713 3.81295 1.90585C2.36533 3.13699 1.40324 4.84324 1.09896 6.71907C0.794684 8.5949 1.1681 10.5178 2.15233 12.1434C3.13657 13.769 4.66734 14.9912 6.47053 15.591C6.87053 15.664 7.01653 15.417 7.01653 15.205C7.01653 15.015 7.00953 14.512 7.00553 13.845C4.78053 14.328 4.31053 12.772 4.31053 12.772C4.16325 12.2887 3.84837 11.8739 3.42253 11.602C2.69653 11.102 3.47753 11.116 3.47753 11.116C3.73043 11.1515 3.97191 11.2442 4.18365 11.3869C4.39539 11.5297 4.57182 11.7189 4.69953 11.94C4.80755 12.1377 4.95378 12.3119 5.12972 12.4526C5.30567 12.5933 5.50782 12.6976 5.72442 12.7595C5.94103 12.8214 6.16778 12.8396 6.39148 12.813C6.61519 12.7865 6.83139 12.7158 7.02753 12.605C7.06381 12.1992 7.24399 11.8197 7.53553 11.535C5.75953 11.335 3.89153 10.647 3.89153 7.581C3.88005 6.78603 4.17513 6.01716 4.71553 5.434C4.47318 4.74369 4.50322 3.98693 4.79953 3.318C4.79953 3.318 5.47053 3.103 6.99953 4.138C8.31074 3.77905 9.69432 3.77905 11.0055 4.138C12.5325 3.103 13.2055 3.318 13.2055 3.318C13.5012 3.9877 13.5294 4.74513 13.2845 5.435C13.8221 6.01928 14.114 6.78817 14.0995 7.582C14.0995 10.655 12.2285 11.332 10.4465 11.53C10.6375 11.724 10.7847 11.9566 10.8784 12.2123C10.972 12.4679 11.0099 12.7405 10.9895 13.012C10.9895 14.081 10.9795 14.944 10.9795 15.206C10.9795 15.42 11.1235 15.669 11.5295 15.591C13.3328 14.9911 14.8636 13.7689 15.8479 12.1432C16.8321 10.5175 17.2054 8.59447 16.901 6.71858C16.5966 4.84268 15.6343 3.13642 14.1865 1.90536C12.7387 0.674306 10.9 -0.0011359 8.99953 1.43397e-06Z" fill="white"/>
107-
</svg>
108-
</a>
104+
{{ $sourceUrl := index $tab "sourceUrl" }}
105+
{{ if $sourceUrl }}
106+
<a href='{{ $sourceUrl }}' tabindex="1" class="button text-neutral-400 hover:text-slate-100 h-6 w-6 p-1" title="Improve this code example">
107+
<svg width="18" height="16" viewBox="0 0 18 16" fill="github" xmlns="http://www.w3.org/2000/svg">
108+
<path fill-rule="evenodd" clip-rule="evenodd" d="M8.99953 1.43397e-06C7.09918 -0.000897921 5.26058 0.674713 3.81295 1.90585C2.36533 3.13699 1.40324 4.84324 1.09896 6.71907C0.794684 8.5949 1.1681 10.5178 2.15233 12.1434C3.13657 13.769 4.66734 14.9912 6.47053 15.591C6.87053 15.664 7.01653 15.417 7.01653 15.205C7.01653 15.015 7.00953 14.512 7.00553 13.845C4.78053 14.328 4.31053 12.772 4.31053 12.772C4.16325 12.2887 3.84837 11.8739 3.42253 11.602C2.69653 11.102 3.47753 11.116 3.47753 11.116C3.73043 11.1515 3.97191 11.2442 4.18365 11.3869C4.39539 11.5297 4.57182 11.7189 4.69953 11.94C4.80755 12.1377 4.95378 12.3119 5.12972 12.4526C5.30567 12.5933 5.50782 12.6976 5.72442 12.7595C5.94103 12.8214 6.16778 12.8396 6.39148 12.813C6.61519 12.7865 6.83139 12.7158 7.02753 12.605C7.06381 12.1992 7.24399 11.8197 7.53553 11.535C5.75953 11.335 3.89153 10.647 3.89153 7.581C3.88005 6.78603 4.17513 6.01716 4.71553 5.434C4.47318 4.74369 4.50322 3.98693 4.79953 3.318C4.79953 3.318 5.47053 3.103 6.99953 4.138C8.31074 3.77905 9.69432 3.77905 11.0055 4.138C12.5325 3.103 13.2055 3.318 13.2055 3.318C13.5012 3.9877 13.5294 4.74513 13.2845 5.435C13.8221 6.01928 14.114 6.78817 14.0995 7.582C14.0995 10.655 12.2285 11.332 10.4465 11.53C10.6375 11.724 10.7847 11.9566 10.8784 12.2123C10.972 12.4679 11.0099 12.7405 10.9895 13.012C10.9895 14.081 10.9795 14.944 10.9795 15.206C10.9795 15.42 11.1235 15.669 11.5295 15.591C13.3328 14.9911 14.8636 13.7689 15.8479 12.1432C16.8321 10.5175 17.2054 8.59447 16.901 6.71858C16.5966 4.84268 15.6343 3.13642 14.1865 1.90536C12.7387 0.674306 10.9 -0.0011359 8.99953 1.43397e-06Z" fill="white"/>
109+
</svg>
110+
</a>
111+
{{ end }}
109112
</div>
110113
{{ end }}
111114
</div>

local_examples/HashExample.cs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// EXAMPLE: hash_tutorial
2+
// HIDE_START
3+
4+
using NRedisStack.Tests;
5+
using StackExchange.Redis;
6+
7+
//REMOVE_START
8+
namespace Doc;
9+
[Collection("DocsTests")]
10+
//REMOVE_END
11+
public class HashExample
12+
// REMOVE_START
13+
: AbstractNRedisStackTest, IDisposable
14+
// REMOVE_END
15+
{
16+
// REMOVE_START
17+
public HashExample(EndpointsFixture fixture) : base(fixture) { }
18+
19+
[SkippableFact]
20+
// REMOVE_END
21+
public void run()
22+
{
23+
//REMOVE_START
24+
// This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection
25+
SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone);
26+
var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone);
27+
//REMOVE_END
28+
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
29+
var db = muxer.GetDatabase();
30+
db.KeyDelete("bike:1");
31+
//HIDE_END
32+
//STEP_START set_get_all
33+
// Zuggle
34+
db.HashSet("bike:1", new HashEntry[]
35+
{
36+
new HashEntry("model", "Deimos"),
37+
new HashEntry("brand", "Ergonom"),
38+
new HashEntry("type", "Enduro bikes"),
39+
new HashEntry("price", 4972)
40+
});
41+
42+
Console.WriteLine("Hash Created");
43+
// Hash Created
44+
45+
var model = db.HashGet("bike:1", "model");
46+
Console.WriteLine($"Model: {model}");
47+
// Model: Deimos
48+
49+
var price = db.HashGet("bike:1", "price");
50+
Console.WriteLine($"Price: {price}");
51+
// Price: 4972
52+
53+
var bike = db.HashGetAll("bike:1");
54+
Console.WriteLine("bike:1");
55+
Console.WriteLine(string.Join("\n", bike.Select(b => $"{b.Name}: {b.Value}")));
56+
// Bike:1:
57+
// model: Deimos
58+
// brand: Ergonom
59+
// type: Enduro bikes
60+
// price: 4972
61+
//STEP_END
62+
63+
//REMOVE_START
64+
Assert.Equal(4, bike.Length);
65+
Assert.Equal("Deimos", model);
66+
Assert.Equal(4972, price);
67+
//REMOVE_END
68+
69+
//STEP_START hmget
70+
var values = db.HashGet("bike:1", new RedisValue[] { "model", "price" });
71+
Console.WriteLine(string.Join(" ", values));
72+
// Deimos 4972
73+
//REMOVE_START
74+
Assert.Equal("Deimos", values[0]);
75+
Assert.Equal(4972, values[1]);
76+
//REMOVE_END
77+
//STEP_END
78+
79+
//STEP_START hincrby
80+
var newPrice = db.HashIncrement("bike:1", "price", 100);
81+
Console.WriteLine($"New price: {newPrice}");
82+
//REMOVE_START
83+
Assert.Equal(5072, newPrice);
84+
//REMOVE_END
85+
// New price: 5072
86+
87+
newPrice = db.HashIncrement("bike:1", "price", -100);
88+
Console.WriteLine($"New price: {newPrice}");
89+
//REMOVE_START
90+
Assert.Equal(4972, newPrice);
91+
//REMOVE_END
92+
// New price: 4972
93+
//STEP_END
94+
95+
//STEP_START incrby_get_mget
96+
var rides = db.HashIncrement("bike:1", "rides");
97+
Console.WriteLine($"Rides: {rides}");
98+
//REMOVE_START
99+
Assert.Equal(1, rides);
100+
//REMOVE_END
101+
// Rides: 1
102+
103+
rides = db.HashIncrement("bike:1", "rides");
104+
Console.WriteLine($"Rides: {rides}");
105+
//REMOVE_START
106+
Assert.Equal(2, rides);
107+
//REMOVE_END
108+
// Rides: 2
109+
110+
rides = db.HashIncrement("bike:1", "rides");
111+
Console.WriteLine($"Rides: {rides}");
112+
//REMOVE_START
113+
Assert.Equal(3, rides);
114+
//REMOVE_END
115+
// Rides: 3
116+
117+
var crashes = db.HashIncrement("bike:1", "crashes");
118+
Console.WriteLine($"Crashes: {crashes}");
119+
//REMOVE_START
120+
Assert.Equal(1, crashes);
121+
//REMOVE_END
122+
// Crashes: 1
123+
124+
var owners = db.HashIncrement("bike:1", "owners");
125+
Console.WriteLine($"Owners: {owners}");
126+
//REMOVE_START
127+
Assert.Equal(1, owners);
128+
//REMOVE_END
129+
// Owners: 1
130+
131+
var stats = db.HashGet("bike:1", new RedisValue[] { "crashes", "owners" });
132+
Console.WriteLine($"Bike stats: crashes={stats[0]}, owners={stats[1]}");
133+
//REMOVE_START
134+
Assert.Equal(1, stats[0]);
135+
Assert.Equal(1, stats[1]);
136+
//REMOVE_END
137+
// Bike stats: crashes=1, owners=1
138+
//STEP_END
139+
//HIDE_START
140+
}
141+
}
142+
//HIDE_END

local_examples/local_hash_demo.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# EXAMPLE: local_hash_demo
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
6+
# HIDE_END
7+
8+
# STEP_START hset_hget
9+
res = r.hset("user:1000", "name", "John Smith")
10+
print(res)
11+
# >>> 1
12+
res = r.hset("user:1000", mapping={"email": "[email protected]", "age": "30"})
13+
print(res)
14+
# >>> 2
15+
res = r.hget("user:1000", "name")
16+
print(res)
17+
# >>> John Smith
18+
res = r.hgetall("user:1000")
19+
print(res)
20+
# >>> {'name': 'John Smith', 'email': '[email protected]', 'age': '30'}
21+
# REMOVE_START
22+
assert res["name"] == "John Smith"
23+
r.delete("user:1000")
24+
# REMOVE_END
25+
# STEP_END

local_examples/local_list_demo.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// EXAMPLE: local_list_demo
2+
// HIDE_START
3+
package main
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"github.com/go-redis/redis/v8"
9+
)
10+
11+
func main() {
12+
rdb := redis.NewClient(&redis.Options{
13+
Addr: "localhost:6379",
14+
})
15+
ctx := context.Background()
16+
// HIDE_END
17+
18+
// STEP_START lpush_lrange
19+
err := rdb.LPush(ctx, "mylist", "world").Err()
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
err = rdb.LPush(ctx, "mylist", "hello").Err()
25+
if err != nil {
26+
panic(err)
27+
}
28+
29+
vals, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
30+
if err != nil {
31+
panic(err)
32+
}
33+
fmt.Println(vals)
34+
// >>> [hello world]
35+
// REMOVE_START
36+
rdb.Del(ctx, "mylist")
37+
// REMOVE_END
38+
// STEP_END
39+
40+
// HIDE_START
41+
}
42+
// HIDE_END

local_examples/local_string_demo.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// EXAMPLE: local_string_demo
2+
// HIDE_START
3+
const redis = require('redis');
4+
const client = redis.createClient();
5+
6+
await client.connect();
7+
// HIDE_END
8+
9+
// STEP_START set_get
10+
let res = await client.set('mykey', 'Hello Redis!');
11+
console.log(res);
12+
// >>> OK
13+
res = await client.get('mykey');
14+
console.log(res);
15+
// >>> Hello Redis!
16+
// REMOVE_START
17+
console.assert(res === 'Hello Redis!');
18+
await client.del('mykey');
19+
// REMOVE_END
20+
// STEP_END
21+
22+
// STEP_START incr
23+
res = await client.set('counter', '10');
24+
console.log(res);
25+
// >>> OK
26+
res = await client.incr('counter');
27+
console.log(res);
28+
// >>> 11
29+
res = await client.incrBy('counter', 5);
30+
console.log(res);
31+
// >>> 16
32+
// REMOVE_START
33+
console.assert(res === 16);
34+
await client.del('counter');
35+
// REMOVE_END
36+
// STEP_END

local_examples/local_string_demo.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# EXAMPLE: local_string_demo
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
6+
# HIDE_END
7+
8+
# STEP_START set_get
9+
res = r.set("mykey", "Hello Redis!")
10+
print(res)
11+
# >>> True
12+
res = r.get("mykey")
13+
print(res)
14+
# >>> Hello Redis!
15+
# REMOVE_START
16+
assert res == "Hello Redis!"
17+
r.delete("mykey")
18+
# REMOVE_END
19+
# STEP_END
20+
21+
# STEP_START incr
22+
res = r.set("counter", "10")
23+
print(res)
24+
# >>> True
25+
res = r.incr("counter")
26+
print(res)
27+
# >>> 11
28+
res = r.incr("counter", 5)
29+
print(res)
30+
# >>> 16
31+
# REMOVE_START
32+
assert res == 16
33+
r.delete("counter")
34+
# REMOVE_END
35+
# STEP_END

0 commit comments

Comments
 (0)