Redis (REmote DIctionary Server) is a Key-value storage system written by Salvatore Sanfilippo.
Redis is an open source API that is written in ANSI C, adheres to the BSD protocol, supports networks, can be persisted in memory, key-value databases, and provides multiple languages.
It is commonly referred to as a data structure server, because the value can be a string (string), a hash (MAP), a list, a collection (sets), and an ordered collection (sorted sets).
Redis:https://github.com/MSOpenTech/redis/releases.
According to the actual situation of your system platform, choose the corresponding version, for example: in the Windows environment can download redis-x64-xxx.zip version, unzip to C Drive and rename the folder for Redis to use (for example, C: \ Redis).
Start the server-open the CMD window to switch the path to C:\redis, enter the command:redis-server.exe redis.windows.conf start the Redis service side.
To start the client-also open a CMD window and switch the path to C:\redis, enter the command: redis-cli.exe-h 127.0.0.1-p 6379 The client starts, we can then use the client via Redis command.
For example:
Set the key-value pair set Firstkey Hello
Remove key value pair get firstkey
Redis is used as a cache in Java, and common operations are enumerated as a shared method:
ImportJava.util.concurrent.TimeUnit;Importorg.springframework.beans.factory.annotation.Autowired; Importorg.springframework.data.redis.core.RedisTemplate; Importorg.springframework.data.redis.core.StringRedisTemplate; Importorg.springframework.data.redis.core.ValueOperations; Importorg.springframework.stereotype.Component; @Component Public classRedissentinelutil {@Autowired//the template,stringredistemplate of the action string is a subset of the Redistemplate Privatestringredistemplate stringredistemplate; @Autowired//redistemplate, you can do all the work PrivateRedistemplate<object,object>redistemplate; Public voidSetcachesdata (byte[] key, Object value) {Redistemplate.opsforvalue (). Set (key, value); } /*** Set the value and setting the Expiration time *@paramKey required to save the key *@paramvalue to be saved *@paramTimeout Expiry time *@return */ Public voidSetcachesdata (byte[] key, Object value,intTimeOut) {Redistemplate.opsforvalue (). Set (Key, Serializautil.serialize (value), timeout,timeunit.seconds); } Public voidsetcachesdata (string key, String value) {Stringredistemplate.opsforvalue (). Set (key, value); } Public Booleansetnx (string key, String value) {returnStringredistemplate.opsforvalue (). Setifabsent (key, value); } Public voidSetcachesdata (string key, String value,intTimeOut) {valueoperations<string, string> Ops = This. Stringredistemplate.opsforvalue (); BooleanBexistent = This. Stringredistemplate.haskey (key); if(bexistent) {System.out.println ("This key is bexistent!"); }Else{ops.set (key, value, timeout,timeunit.seconds); } } /*** key is present *@paramKey required to verify the key *@returnis*/ PublicBoolean Isexistskey (String key) {returnStringredistemplate.haskey (key); } /*** key is present *@paramKey required to verify the key *@returnis*/ PublicBoolean Isexistskey (byte[] key) { returnRedistemplate.haskey (key); } /*** Invalid * *@paramKey Saved by key *@paramseconds sec*/ PublicBoolean Setxpiredata (String key,intseconds) { returnStringredistemplate.expire (key, seconds,timeunit.seconds); } /*** Invalid *@paramKey Saved by key *@paramseconds sec*/ PublicBoolean Setxpiredata (byte[] Key,intseconds) { returnRedistemplate.expire (key, seconds,timeunit.seconds); } /*** Get data from the cache *@paramKey *@return */ Publicstring GetValue (String key) {if(Key = =NULL) return""; String v=Stringredistemplate.opsforvalue (). get (key); return(v = =NULL) ? "": v; } /*** Get data from the cache *@paramKey *@return */ Publicstring[] getValues (String key) {if(Key = =NULL) return NULL; if(Stringredistemplate.opsforvalue (). Get (key)! =NULL) {string[] s= Stringredistemplate.opsforvalue (). Get (Key). Split (","); if(NULL!=s && s.length>0) returns; } return NULL; } /*** Get data from the cache *@paramKey *@return */ PublicObject GetValue (byte[] key) { returnRedistemplate.opsforvalue (). get (key); } /*** Delete data from cache *@paramKey *@return */ Public voidremovevalue (String key) {stringredistemplate.delete (key); } /*** Delete data from cache *@paramKey *@return */ Public voidRemoveValue (byte[] key) {Redistemplate.delete (key); }}
Application of Redis in Windows environment