1

I want to do a fuzzy search in Redis

I have lots of Domain-IP info to maintain, and I want to use a hash to hold them

The structure of Domain-IP is sth like this:

Domain-IP

domain

ip

last-access (last time I use this ip to access the domain)

access-count

...

Now I want to do two things:

  1. search this Domain-IP info by domain, sth like: select * from domain-ip where domain = "www.google.com"

  2. update Domain-IP info by domain and IP, sth like: update domain-ip set access-count = access-count + 1 where domain = "www.google.com" and ip = "192.168.1.1"

so my design the structure in Redis is:

HSET domainip.www.google.com.192.168.1.1 access-count 20

HSET domainip.www.google.com.192.168.1.2 access-count 20

so I can serach them by domain like this, for example, search info about www.google.com:

  1. get all keys starts with www.google.com by : keys domainip.www.google.com*
  2. use these keys to get all the info about google in a loop:
for (string key : keys) {
   execute("HGETALL " + key) // then convert the hash into a POJO
}

and the update operation as follow:

"HSET domainip." + domainStr + "." + ipStr + " access-count " + newAccessCount;

"HSET domainip." + domainStr + "." + ipStr + " last-access " + newLastAccess;

My question is :

  1. Does the loop causes performance issue?

  2. Is that right to update the hash filed one by one?

It will be nice if you can provide a good idea to complete the operation what I want in Redis, Thx :D

4

1 回答 1

1

不建议将密钥用于生产用途(请参阅http://redis.io/commands/keys)。我的第一直觉是,您实际上想要一个 SQL 数据库,Redis 不会像您描述的那样对数据进行任意切片。

如果出于某种原因确实需要使用 redis,则需要维护一堆索引以供访问。基本上,您要使用的每个“where 子句”都将获得一个集合(或排序集合,如果您需要特定的排序),维护与该键匹配的哈希列表。因此,例如,如果您有哈希

domainip.www.google.com.192.168.1.1
domainip.www.google.com.192.168.1.2
domainip.www.yahoo.com.192.168.1.4

您将制作索引集:

by_domain:www.google.com => (
         domainip.www.google.com.192.168.1.1,
         domainip.www.google.com.192.168.1.2
)
by_domain:www.yahoo.com => (
         domainip.www.yahoo.com.192.168.1.4
)

其中集合的值是哈希的键,集合的键是<column>:<value>格式的查询。在添加和删除哈希键时,您需要维护这些索引。

于 2013-10-24T20:47:17.493 回答