【发布时间】:2014-05-08 12:45:00
【问题描述】:
我真的很喜欢 redis 和 mysql,并广泛使用它们。我有兴趣从我的 redis 实例中清除不再需要的某些键,因为内存很昂贵。我想把它停在磁盘上并永远留在那里。我不是很讲究如何,但我正在探索将它放在mysql中。对于大多数 redis 数据类型来说,这是小菜一碟。它要么是字符串,要么是 json 编码可以轻松处理的东西。我的问题是位图,它是数据的二进制表示。
这是我天真的 psydo 代码/python 方法:
#create an arbitrary bitmap with every third bit ticked to 1
for i in range(100):
rediscon.setbit('thekey',i*3, 1)
#get the value as a string
thevalue = rediscon.get('thekey')#is get appropriate for a bitmap?
#this looks something like "@ \b\x02\x00\x80 "
#do the insert into mysql
mysql.query("insert into table (key, value) VALUES ('thekey', "+MySQLdb.escape_string(thevalue)+")")
#do a sanity check, restore the key back to redis
#get the value from mysql and put it back in redis with a new key
val = MySQLdb.query("select value from table where key='thekey'")
rediscon.set('thekey_new', val)
print rediscon.bitcount('thekey')#this prints correctly as 100
print rediscon.bitcount('thekey_new')#this is wrong, it prints a number much less than 100
mysql 引擎类型是 myisam,我认为这并不重要。我已经为values 的列类型尝试了 BLOB 和 LONGTEXT。我想在位图上做get 吗?如何在 mysql 中插入二进制数据?
我该怎么做?我希望能够把这个位图放到mysql中,以后有能力恢复。谢谢!
【问题讨论】:
标签: mysql redis binary-data