Help us understand the problem. What is going on with this article?

python+redisで、Base91のキーを扱う。

More than 1 year has passed since last update.

Redisのキーの長さをお手軽に短くしたい。

とある業務上の要請で、Redisに数兆種類のデータを突っ込めるようにしたい。
その際にキーをなるべく短い文字長にしたい。
...「Base N」encodingまとめを眺めて気がついた。

それ, Base91を使えば7文字でいけるよ。

91*91*91*91*91*91 >90*90*90*90*90*90
=531,441,000,000 > 500B = 5000億通り

だから、先頭一文字をアルファベットに限ったとしても、兆のケタのキー種類を表現できる。
以下、同じようなことをやらなければならなくなった(超)忙しい人なんか向けたレポートを残しておく。
redisにはpython2(2.7.13)経由でアクセス。

『Base91 encoding』で使われる文字列

以下の通り:

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

pythonで定義するならば。

こうかな。

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

redisへのBase91文字列投入試験。

redisを立ち上げて、書捨てこーどを書く。

python2コード

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 

結果

データをredisにハッシュのキーバリューとして投入:

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

redisからのBase91キーバリューの読み出し:

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    

ターミナル側から:

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

オッケー♪

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away