i am using StackExchange.Redis client to store my data in redis server
but while trying to insert key as Integer
redisDataBase.StringSet(1,"1");
i got exception , a though in redis documentation, Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like "foo" to the content of a JPEG file. The empty string is also a valid key.
It is because int cannot be the key, the key should be the type of string or byte[]. You can check out this page to see more.
And if you want an integer to be a key, you could convert it to a String
redisDataBase.StringSet("1","mohammed")
Related
I would like to have a function in sql that encrypts a given varchar value and returns it as a varchar and Vice-versa. I have checked out the following functions of sql such as :
ENCRYPTBYPASSPHRASE ENCRYPTBYKEY ENCRYPTBYCERT
I have also checked out other questions here but I am unable to find (or maybe understand) the solution I am looking for. What I want to do is pass the encrypted string to a URL as query string (I would like this to do this from Db) which does not have any / so that it does not mess with the URL routes.
Step 1: I made random string generator. (Works)
DECLARE #RandomString VARCHAR (500) = ( SELECT RandomString
FROM dbo.SfRandomStringGenerator (64, 1) );
Example string:
X922t1N2udpdi30HZN9W4U9N997UatHZMJKWvI4si0w9g9q6FA3Lqd8NxCJXAe5D
Step 2: I would like to encrypt the string from Step 1 so that the Output is also in String format BUT the Encryption methods that I have come across all return VARBINARY which is not what I Want.
Declare #EncryptedString Nvarchar(MAX)=dbo.SfEncrypt(#RandomString)
Select #EncryptedString
Example string:
WDkyMnQxTjJ1ZHBkaTMwSFpOOVc0VTlOOTk3VWF0SFpNSktXdkk0c2kwdzlnOXE2RkEzTHFkOE54Q0pYQWU1RA
Step 3: I would also like to be able to decrypt the encrypted string.
Declare #DecryptedString Nvarchar(MAX)=dbo.SfDecrypt(#RandomString)
Select #DecryptedString
Example string:
X922t1N2udpdi30HZN9W4U9N997UatHZMJKWvI4si0w9g9q6FA3Lqd8NxCJXAe5D
Thus far I have not been able to get the encryption I want.
Any help or pointers towards the solution would be helpful. Thanks.
Read the VARBINARY result into a byte[] array.
Convert the byte[] array into a Base 64 string using System.Convert.ToBase64String()
Base64 includes the + and / characters which have special meaning in a url path, so they must be encoded.
UrlEncode the base 64 string using System.Web.HttpUtility.UrlEncode(string) to encode any + or / characters with %2B or %2F.
You should then be able to add the url-encoded string to your url.
On the url endpoint, reverse the procedure to obtain the original encrypted byte[] array.
EDIT: Be aware that using a query string can come up against a length limit, of the order of 2048 characters, so if your data is larger than this, you may find that the server will refuse to handle the request.
In that case, consider using POST to send your data, and supply the encrypted data in the body of the message request.
EDIT in response to comment:
I figured that you would be processing the result of your "do encryption" query before sending it to a server as a url, and so the fact that the SQL encryption functions return varbinary should not have presented a problem.
IF you are happy that you can actually do encryption and decryption in TSQL, then I'll refer you to this SO post which offers a way to do Base-64 encoding and decoding in TSQL care of the xml query api. Do your encryption to a varbinary field, run it through the example to generate a base64 varchar, and then use TSQL REPLACE to convert '+' to '%2B' and '/' to '%2F'
You'll then have a varchar value of a url-encoded, base-64 encoded, encrypted representation of your input data, safe for transmission as a query string.
When the key contains - in the key, i.e.
e: = conn.LPush("6cd3b647-0a9d-4119-6438-07a9dda1bc7f", json)
it gives:
WRONGTYPE Operation against a key holding the wrong kind of
value
The 'WRONGTYPE' error means that the key already has a value but of another type. In your case, the key isn't a list so you can LPUSH into it.
You can find the key's type with the TYPE command. If you want to create a list in that key, first DEL the existing one.
After I imported SJCL AES key using new sjcl.cipher.aes(rawKey), how can I get rawKey value back from the key instance?
As far as I know it is not easily possible.
Looking at the code, the key is immediatly transformed using the S-Box. The result ist stored in this._key, so you could get that result and then use the reverse S-Box to restore rawKey. This should work, but just storing the key in a variable is of course way simpler.
In Jedis, I want to set some key and value with expiring time by a single invocation.
I can do this by combination of set() and expire() but it needs two invocations.
I found the following method:
set(final String key, final String value, final String nxxx, final String expx, final long time)
But I have to choose nx (Only set the key if it does not already exist.) or xx (Only set the key if it already exist.).
I want to cover both cases.
Any suggestion? Or any reason to be written like this?
Redis has a SETEX command, which sets the key with an expiry.
jedis.setex(key, expireAfter, value);
This question is so misleading. nx and xx are indeed for different use cases and mutually exclusive. If you want to simply overwrite any expiry, simply don't pass in none of below:
NX -- Set the key only when the key doesn't exist
XX -- Set the key only when the key has existed
I tried adding some sample score-value pairs to redis sorted set using below code:
String key = "set";
redis.zadd(key, 5, "1034");
redis.zadd(key, 2, "1030");
redis.zadd(key, 1, "1089");
and tried retrieving it using byteArray and BitSet
byte[] byteArr = redis.get(key.getBytes());
BitSet bitSet = fromByteArrayReverse(byteArr);
System.out.println(bitset.toString()));
also i tried executing
System.out.println(redis.get(key.getBytes()));
which is supposed to give me an address of the byte-array
But for both of these commands i get the error
" ERR Operation against a key holding the wrong kind of value"
So can anyone please tell me why does this error occur in the first place and also the correct redis command/code to retrieve values from a redis sorted-set??
What you want is calling
ZSCORE key "1034"
Or in the case of wanting only elements between two particular scores
ZRANGEBYSCORE key lower upper
Since you also have "rank" (position or index, as in a list) you can also ask for example for the first three elements in your set
ZRANGEBYRANK key 0 2
The error you are getting is because once you assign a value to a key, that value defines the type of the internal structure on redis, and you can only use commands for that particular structure (or generic key commands such as DEL and so on). In your case you are trying to mix sorted sets with byte operations and it doesn't match.
To see all sorted set commands, please refer to http://redis.io/commands#sorted_set