use redisearch with openresty - redis

I would like to use openresty to connect to redisearch.
The are no available specific clients for Lua, so i was thinking to use the native lua-resty-redis
How can i execute this redis command with lua?
127.0.0.1:6379> FT.SEARCH myIdx "hello world" LIMIT 0 10

reply to self:
i've added this function in redis.lua
function _M.ftsearch(self, ...)
return _do_cmd(self, "ft.search", ...)
end

Related

How to check the value in redis stored by spring boot #cacheable

Sorry, I'm new to the combination of redis and springboot #cacheable.
I just stored some data with springboot #cacha in redis and then I tried to check the value from redis-cli.
However, I couldn't get the value with the key, it's always null even though the API can get the value from the redis.
Here is my code:
// conde in springboot controller
#GetMapping("/")
#ResponseBody
#Cacheable(value = "dataList")
public SomeObject getDataList(SomeParameters someParameters){ ... }
# code I used to check the data in redis
$ 127.0.0.1:6379>keys *
1) "dataList::SimpleKey []"
$ 127.0.0.1:6379>llen dataList
0
$ 127.0.0.1:6379>type dataList
none
$ 127.0.0.1:6379>type dataList::SimpleKey
none
I'm wondering how to get value with that key above...
Thank you in advance.
Oh, I just figured it out on my own. It turns to be that entire value inside of double quotation is the key!
So the correct way to get the value is:
$ 127.0.0.1:6379>keys *
1) "dataList::SimpleKey []"
$ 127.0.0.1:6379>get "dataList::SimpleKey []"
Hope it could help you if you had the same confusion!

Can django-redis use dbsize?

django-redis source: https://github.com/jazzband/django-redis/tree/master/django_redis
my problem is I can not find method to get number of keys in Redis database, it call dbsize. Methods that available such as set, get, add, delete, delete_pattern, delete_many, clear, get_many, set_many, incr, decr, has_keys, keys, iter_keys, ttl, pttl, persist, expire, expire_at, pexpire, pexpire_at, lock, close, touch.
How can I used dbsize method of redis command in django-redis library?
environment:
django version : 3.2.10
django-redis: 5.2.0
I found the solution of the question
from django_redis import get_redis_connection
REDIS = get_redis_connection("default") # default is alias of redis
REDIS.dbsize() # get number of keys in the currently-selected database
this solution can use native redis command but cannot use method of django-redis plugin
WARNING: Not all pluggable clients support this feature.

Spring data redis zadd command missed nx|xx|incr options

Lettuce supported zadd's NX|XX|CH|INCR options on 2015 link.
But I can't find any thing support this in Lettuce's wrapper Spring data Redis (version:2.1.5).
It seem that the only two zadd methods that DefaultZSetOperations provide can't let me use NX|XX|CH|INCR options:
Boolean add(K key, V value, double score);
Long add(K key, Set<TypedTuple<V>> tuples);
So how can I use NX|XX|CH|INCR options on Spring data redis with Lettue?
Sorry for my poor english ,Thanks.
Not completely sure if this will 100% work the same for Lettuce. For Jedis, found that we have to use redisTemplate.execute(RedisCallback). A small example of using the ch argument for indicating if any records where changed (opposed to just being added to the sorted set)
redisTemplate.execute(
(RedisCallback<Long>) connection -> connection.zSetCommands().zAdd(
leaderboardKey.getBytes(StandardCharsets.UTF_8),
membersToTuples(members),
RedisZSetCommands.ZAddArgs.empty().ch()
)
)

Idiomatic approach to conditional update of key

I'd like to use Redis to cache the most recent piece of data that a user has sent to me. However, I can't just use SET, because the user may send data out of order, I need to condition the SET based on the value of another key, e.g.:
latest_timestamp = GET "latest_timestamp:<new_data.user_id>"
if latest_timestamp < new_data.timestamp {
SET "latest_timestamp:<new_data.user_id>" new_data.timestamp
SET "latest_data:<new_data.user_id>" new_data.to_string()
}
What is the idiomatic way to handle this situation?
A server-side Lua script (see EVAL) is the idiomatic-est approach IMO.
Make sure that your code passes the full names (i.e. does all substitutions) of both keys, as well as the new timestamp and the new data as arguments. The script should look something like this:
local lts = tonumber(redis.call('GET', KEYS[1]))
local nts = tonumber(ARGV[1])
if lts < nts then
redis.call('SET', KEYS[1], nts)
redis.call('SET, KEYS[2], ARGV[2])
end

Why are my package (our'd) variables getting cleared out between PerlChildInitHandler and PerlResponseHandler in mod_perl2?

I have mod_perl2 running on a virtual host and I'm trying to make my mysql connection persistent between requests to handle server load. I have read all of the documentation and a book on the topic and I still have no idea why this bare-bones implementation of a mod_perl2 web application replies with "It's broken!".
package Test;
use strict;
use warnings;
use Apache2::Const;
use Carp qw{croak};
use DBI;
our $mysql_handle;
sub handler {
print "Content-Type: text/plain\n\n";
print (defined $mysql_handle ? "It's defined!" : "It's broken!");
return Apache2::Const::OK;
}
sub child_init {
my ($db, $host, $port, $user, $pass)
= qw{app_db localhost 3306 app_user app_pass};
$mysql_handle
= DBI->connect("dbi:mysql:database=$db;host=$host;port=$port", $user, $pass)
or croak("Failed to establish a connection with mysqld: $DBI::errstr");
return Apache2::Const::OK;
}
1;
This is very strange and makes no sense at all to me. It's as if $mysql_handle is lexically-scoped -- when it's not! Please, can some one explain this to me?
You should look at Apache::DBI for mysql connection persistance in mod_perl. It overloads DBI's connect and disconnect which allows you to use DBI->connect(...) normally, with the added benefit of the code working in or out of a mod perl environment.
As far as the scoping issue, I'd need a little more feedback on your mp setup. I would try use vars '$mysql_handle' or even $Test::mysql_handle = DBI->connect(...) and see if you don't get the results you are looking for.