Handle Base91 keys with python + redis.

I want to easily shorten the length of the Redis key.

I want to be able to put trillions of types of data into Redis at a certain business request. At that time, I want to make the key as short as possible. ... "Base N" encoding summary I noticed.

If you use Base91, you can use 7 characters.

919191919191 >909090909090 = 531,441,000,000> 500B = 500 billion ways

Therefore, even if the first character is limited to the alphabet, it is possible to express the key type of trillion digits. Below, I'll leave a report for someone who is (super) busy who has to do something similar. Access redis via python2 (2.7.13).

Character string used in "Base91 encoding"

As follows: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"

If defined in python.

Is it like this?

baseNN.py


base90 ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"
base91=base90 + '"'

Base91 string input test to redis.

Launch redis and write abandoned cards.

python2 code

test_base91.py


from __future__ import absolute_import
from __future__ import unicode_literals

import redis
import time

r = redis.StrictRedis(host='localhost', port=6379, db=0)

#Base91 encoding 
#ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"
base90 ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"
base91=base90 + '"'

#for baseNN (1 <= N <= 95) 
baseNN=base91
LNN = len(baseNN)
test ="test!#$%&()*+,./:;<=>?@[]^_`{|}~"

start = time.time()
print "Insert to redis:"
with r.pipeline() as pipe:
    for key in range(LNN):
        val = baseNN[key]
        #cf "print string," http://www.lifewithpython.com/2013/12/python-print-without-.html
        print key, val, ";",
        pipe.hset(test, key,val)
        pipe.hset(test, val,key)
    pipe.execute()
stop = time.time()

def test_hget(k):
    ret = r.hget(test,k)
    print k,"->",ret, "   ",
print
print "time",(stop - start)
print "Test reads:" 

for key in range(LNN):
    test_hget(key)
    val = baseNN[key]
    test_hget(val)
    print 

result

Put data into redis as hash key value:

Insert to redis: 0 A ; 1 B ; 2 C ; 3 D ; 4 E ; 5 F ; 6 G ; 7 H ; 8 I ; 9 J ; 10 K ; 11 L ; 12 M ; 13 N ; 14 O ; 15 P ; 16 Q ; 17 R ; 18 S ; 19 T ; 20 U ; 21 V ; 22 W ; 23 X ; 24 Y ; 25 Z ; 26 a ; 27 b ; 28 c ; 29 d ; 30 e ; 31 f ; 32 g ; 33 h ; 34 i ; 35 j ; 36 k ; 37 l ; 38 m ; 39 n ; 40 o ; 41 p ; 42 q ; 43 r ; 44 s ; 45 t ; 46 u ; 47 v ; 48 w ; 49 x ; 50 y ; 51 z ; 52 0 ; 53 1 ; 54 2 ; 55 3 ; 56 4 ; 57 5 ; 58 6 ; 59 7 ; 60 8 ; 61 9 ; 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 " ; time 0.020693063736

Reading Base91 key values from redis:

Testreads:
0 -> 52     A -> 0    
1 -> 53     B -> 1    
2 -> 54     C -> 2    
3 -> 55     D -> 3    
4 -> 56     E -> 4    
5 -> 57     F -> 5    
6 -> 58     G -> 6    
7 -> 59     H -> 7    
8 -> 60     I -> 8    
9 -> 61     J -> 9    
10 -> K     K -> 10    
11 -> L     L -> 11    
12 -> M     M -> 12    
13 -> N     N -> 13    
14 -> O     O -> 14    
15 -> P     P -> 15    
16 -> Q     Q -> 16    
17 -> R     R -> 17    
18 -> S     S -> 18    
19 -> T     T -> 19    
20 -> U     U -> 20    
21 -> V     V -> 21    
22 -> W     W -> 22    
23 -> X     X -> 23    
24 -> Y     Y -> 24    
25 -> Z     Z -> 25    
26 -> a     a -> 26    
27 -> b     b -> 27    
28 -> c     c -> 28    
29 -> d     d -> 29    
30 -> e     e -> 30    
31 -> f     f -> 31    
32 -> g     g -> 32    
33 -> h     h -> 33    
34 -> i     i -> 34    
35 -> j     j -> 35    
36 -> k     k -> 36    
37 -> l     l -> 37    
38 -> m     m -> 38    
39 -> n     n -> 39    
40 -> o     o -> 40    
41 -> p     p -> 41    
42 -> q     q -> 42    
43 -> r     r -> 43    
44 -> s     s -> 44    
45 -> t     t -> 45    
46 -> u     u -> 46    
47 -> v     v -> 47    
48 -> w     w -> 48    
49 -> x     x -> 49    
50 -> y     y -> 50    
51 -> z     z -> 51    
52 -> 0     0 -> 52    
53 -> 1     1 -> 53    
54 -> 2     2 -> 54    
55 -> 3     3 -> 55    
56 -> 4     4 -> 56    
57 -> 5     5 -> 57    
58 -> 6     6 -> 58    
59 -> 7     7 -> 59    
60 -> 8     8 -> 60    
61 -> 9     9 -> 61    
62 -> !     ! -> 62    
63 -> #     # -> 63    
64 -> $     $ -> 64    
65 -> %     % -> 65    
66 -> &     & -> 66    
67 -> (     ( -> 67    
68 -> )     ) -> 68    
69 -> *     * -> 69    
70 -> +     + -> 70    
71 -> ,     , -> 71    
72 -> .     . -> 72    
73 -> /     / -> 73    
74 -> :     : -> 74    
75 -> ;     ; -> 75    
76 -> <     < -> 76    
77 -> =     = -> 77    
78 -> >     > -> 78    
79 -> ?     ? -> 79    
80 -> @     @ -> 80    
81 -> [     [ -> 81    
82 -> ]     ] -> 82    
83 -> ^     ^ -> 83    
84 -> _     _ -> 84    
85 -> `     ` -> 85    
86 -> {     { -> 86    
87 -> |     | -> 87    
88 -> }     } -> 88    
89 -> ~     ~ -> 89    
90 -> "     " -> 90    

From the terminal side:

./redis-cli 127.0.0.1:6379> keys *

  1. "test!#$%&()*+,./:;<=>?@[]^_`{|}~"

Okay ♪

Recommended Posts

Handle Base91 keys with python + redis.
Handle Excel with python
Handle rabbimq with python
[Tips] Handle Athena with Python
Easily handle lists with python + sqlite3
Handle Excel CSV files with Python
Easily handle databases with Python (SQLite3)
Trying to handle SQLite3 with Python [Note]
[python, openCV] base64 Face recognition with images
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Handle zip files with Japanese filenames in Python 3
[Rust / Python] Handle numpy with PyO3 (August 2020 version)
[Python] How to handle Japanese characters with openCV
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
I want to handle optimization with python and cplex
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python