0

How can i override method GET or write a new function which will execute on server side returns another key value if searched key not exists?

Example:

if key "word_1 word_2 word_3 word_4" not exists search this key "word_1 word_2 word_3", if key "word_1 word_2 word_3" not exists search this key "word_1 word_2", if key "word_1 word_2" not exsis search this key "word_1". This recursive search must be in server side (for speed).

Ramin Darvishov
  • 1,024
  • 1
  • 13
  • 24

2 Answers2

1

This must be done in your application layer, there's no infrastructure to do so in Redis, and I believe that never will be part of the product because the point of Redis is being lightweight and blazing fast: the more features you add in the execution pipeline, the more performance you sacrifice....

The Real Bill
  • 12,841
  • 7
  • 33
  • 36
Matías Fidemraizer
  • 59,064
  • 16
  • 107
  • 181
  • Hbase has this option (Observer). preGet, postGet: Called before and after a client makes a Get request. – Ramin Darvishov Dec 04 '15 at 10:02
  • I would recommend using HBase then? – tddmonkey Dec 04 '15 at 10:08
  • @MrWiggles BTW I still suggest you to go with Redis and implement this in your application layer. I don't see why you can't do it: where you issue `GET` commands, you *inject* the code of pre and post operations... – Matías Fidemraizer Dec 04 '15 at 10:10
  • @MatíasFidemraizer Agreed - I was being more facetious than anything - I've posted an answer with a suggested solution for Redis – tddmonkey Dec 04 '15 at 10:12
  • 1. my choice redis. Because Redis holds the whole dataset in memory, but Hbase not 2. Why i do not do on application layer, because i use large dataset and each time when key is not exists in database i must again appeal to datbase. İt is bad network and connection tiime – Ramin Darvishov Dec 04 '15 at 10:21
  • 1
    @RaminDərvişov The point of Redis is that it's very fast. I mean, you can hit it a lot of times unless what you're executing against Redis is long enough. For example, if you implement the LUA script and you extend it more, think that Lua scripts block the entire server. Well, Redis isn't multi-threaded, I ignore if you know it already. – Matías Fidemraizer Dec 04 '15 at 10:24
  • 1
    @RaminDərvişov So it's cheaper to hit it 1k times rather than raising an operation of 1k writes in a Lua script, because between each interruption of sending commands, other commands from other parts of your system can keep communicating with Redis. – Matías Fidemraizer Dec 04 '15 at 10:25
  • @RaminDərvişov Well, when I said "writes" it also applies to "reads". – Matías Fidemraizer Dec 04 '15 at 10:31
1

This functionality isn't built in to Redis, but if you really want this to be done on the server you could use a Lua script to do this for you.

If you don't want to do that an alternative would be to generate the possible keys to search, which in your example would be

word_1 word_2 word_3 word_4
word_1 word_2 word_3
word_1 word_2
word_1

and then issue a MGET to fetch all the keys at once. In your application logic you can just iterate over the results fetching the first one that has a value

tddmonkey
  • 19,324
  • 9
  • 53
  • 66