-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21-cache.sh
More file actions
executable file
·105 lines (86 loc) · 2.8 KB
/
21-cache.sh
File metadata and controls
executable file
·105 lines (86 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
# Example 21: Cache Management
# Demonstrates working with the translation cache
set -e # Exit on error
echo "=== DeepL CLI Example 21: Cache Management ==="
echo
# Check if API key is configured
if ! deepl auth show &>/dev/null; then
echo "❌ Error: API key not configured"
echo "Run: deepl auth set-key YOUR_API_KEY"
exit 1
fi
echo "✓ API key configured"
echo
# Example 1: Check initial cache stats
echo "1. Check initial cache statistics"
deepl cache stats
echo
# Example 2: Perform some translations to populate cache
echo "2. Populate cache with translations"
echo " Translating sample texts..."
deepl translate "Hello, world!" --to es >/dev/null
deepl translate "Good morning" --to fr >/dev/null
deepl translate "How are you?" --to de >/dev/null
deepl translate "Thank you" --to ja >/dev/null
echo " ✓ Translations completed and cached"
echo
# Example 3: Check cache stats after translations
echo "3. Check cache statistics (should show entries now)"
deepl cache stats
echo
# Example 4: Demonstrate cache hit (faster)
echo "4. Demonstrate cache hit (faster translation)"
echo " First translation (cache miss, uses API):"
time deepl translate "The quick brown fox" --to es >/dev/null 2>&1
echo
echo " Second translation (cache hit, instant):"
time deepl translate "The quick brown fox" --to es >/dev/null 2>&1
echo
# Example 5: Check updated cache stats
echo "5. Cache statistics after repeated translation"
deepl cache stats
echo
# Example 6: Disable cache
echo "6. Disable caching"
deepl cache disable
deepl cache stats
echo
# Example 7: Translate with cache disabled (always hits API)
echo "7. Translate with cache disabled (slower)"
echo " Translation without cache:"
time deepl translate "Hello again" --to es 2>&1 | head -5
echo
# Example 8: Re-enable cache
echo "8. Re-enable caching"
deepl cache enable
deepl cache stats
echo
echo "8b. Enable cache with max size limit:"
deepl cache enable --max-size 500M
echo
# Example 9: Clear cache
echo "9. Clear all cached translations"
deepl cache clear --yes
echo
echo "9b. Clear cache with dry-run (preview what would be cleared):"
deepl cache clear --dry-run
echo
echo "9c. Cache stats in JSON format (for scripting):"
deepl cache stats --format json
echo
# Example 10: Verify cache is empty
echo "10. Verify cache is empty"
deepl cache stats
echo
echo "=== All cache examples completed! ==="
echo
echo "💡 Cache benefits:"
echo " - Instant results for repeated translations and write improvements"
echo " - Reduces API calls (saves quota)"
echo " - Works offline for cached translations"
echo " - Default: 1GB max size, 30-day TTL"
echo " - Shared SQLite DB for both translate and write commands"
echo " - Bypass with --no-cache on any translate or write command"
echo
echo "📍 Cache location: ~/.cache/deepl-cli/cache.db (XDG default)"