Common methods for packaging of RedisTemplate
1. Serialization and configuration
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import .Jackson2JsonRedisSerializer; import ; /** * RedisConfiguration * * @author */ @Configuration public class RedisConfiguration { @Bean public RedisSerializer<Object> redisSerializer() { // Create a JSON serializer Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(); ObjectMapper objectMapper = new ObjectMapper(); (, ); // Must be set otherwise JSON cannot be converted into an object and will be converted to Map type (, .NON_FINAL); (objectMapper); return serializer; } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory, RedisSerializer<Object> redisSerializer) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); (redisConnectionFactory); (()); (redisSerializer); (()); (redisSerializer); (); return redisTemplate; } @Bean public RedisService redisService(RedisTemplate<String, Object> redisTemplate) { return new DefaultRedisServiceImpl(redisTemplate); } }
2. Basic constants
package ; import ; /** * RedisConstants * * @author */ public final class RedisConstants { /** * Lua script that increments and sets the expiration time */ public static final DefaultRedisScript<Long> INCR_BY_EXPIRE_LUA_SCRIPT = new DefaultRedisScript<>( "local r = ('INCRBY', KEYS[1], ARGV[1]) ('EXPIRE', KEYS[1], ARGV[2]) return r", ); /** * Lua script that decrements and sets expiration time */ public static final DefaultRedisScript<Long> DECR_BY_EXPIRE_LUA_SCRIPT = new DefaultRedisScript<>( "local r = ('DECRBY', KEYS[1], ARGV[1]) ('EXPIRE', KEYS[1], ARGV[2]) return r", ); private RedisConstants() { } }
3. Interface and implementation
package ; import ; import .slf4j.Slf4j; import .*; /** * AbstractRedisService * * @author */ @Slf4j public abstract class AbstractRedisService implements RedisService { /** * Get Helper class that simplifies Redis data access code. * @return RedisTemplate Get */ protected abstract RedisTemplate<String, Object> getTemplate(); public HashOperations<String, Object, Object> hashOps() { return ().opsForHash(); } public ValueOperations<String, Object> valueOps() { return ().opsForValue(); } public ListOperations<String, Object> listOps() { return ().opsForList(); } public SetOperations<String, Object> setOps() { return ().opsForSet(); } public ZSetOperations<String, Object> zSetOps() { return ().opsForZSet(); } public StreamOperations<String, Object, Object> streamOps() { return ().opsForStream(); } public GeoOperations<String, Object> geoOps() { return ().opsForGeo(); } }
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Redis Service * * @author */ public interface RedisService { /** * Get all keys that match the specified expression * @param pattern expression * @return * @see <a href="/commands/keys" rel="external nofollow" rel="external nofollow" >Keys Command</a> */ Set<String> keys(String pattern); /** * Save properties * @param key key value * @param value value * @param time Time Stamp */ void set(String key, Object value, long time); /** * Save properties * @param key key value * @param value value */ void set(String key, Object value); /** * Get attributes * @param key key value * @return Return object */ Object get(String key); /** * Get attributes in batches from specified keys * @param keys keys * @return values list, when the value is empty, the value corresponding to the key is null * @see <a href="/commands/mget" rel="external nofollow" rel="external nofollow" >MGet Command</a> */ List<Object> mGet(Collection<String> keys); /** * Get the keys attribute in batches and return a map * @param keys keys * @return map, key and value set, when value is obtained as null, this map is not saved */ Map<String, Object> mGetToMap(Collection<String> keys); /** * Delete attributes * @param key key value * @return Return success */ Boolean del(String key); /** * Batch delete attributes * @param keys key value collection * @return Return the number of deletes */ Long del(Collection<String> keys); /** * Set expiration time * @param key key value * @param time Time Stamp * @return Return success */ Boolean expire(String key, long time); /** * Get expiration time * @param key key value * @return Return timestamp */ Long getExpire(String key); /** * Determine whether the key exists * @param key key value * @return Return */ Boolean hasKey(String key); /** * Increment by delta * @param key key value * @param delta delta value * @return Return the result after incrementing */ Long incr(String key, long delta); /** * Press delta to increment and set the expiration time * @param key key value * @param delta delta value * @param timeout Expiry time (unit: seconds) * @return Return the result after incrementing */ Long incrAndExpire(String key, long delta, long timeout); /** * Decrease by delta * @param key key value * @param delta delta value * @return Return the result after decrement */ Long decr(String key, long delta); /** * Press delta to decrement and set the expiration time * @param key key value * @param delta delta value * @param timeout Expiry time (unit: seconds) * @return Return the result after decrement */ Long decrAndExpire(String key, long delta, long timeout); /** * Get the properties in the Hash structure * @param key External key value * @param hashKey internal key value * @return Return the value of the internal key */ Object hGet(String key, String hashKey); /** * Put an attribute into the Hash structure * @param key External key * @param hashKey internal key * @param value The value of the internal key * @param time Expiry time * @return Return whether it succeeds */ Boolean hSet(String key, String hashKey, Object value, long time); /** * Put an attribute into the Hash structure * @param key External key * @param hashKey internal key * @param value The value of the internal key */ void hSet(String key, String hashKey, Object value); /** * Get the entire Hash structure directly * @param key External key value * @return Return hashMap */ Map<Object, Object> hGetAll(String key); /** * Directly set the entire Hash structure * @param key External key * @param map hashMap value * @param time Expiry time * @return Return whether it succeeds */ Boolean hSetAll(String key, Map<String, Object> map, long time); /** * Directly set the entire Hash structure * @param key External key * @param map hashMap value */ void hSetAll(String key, Map<String, ?> map); /** * Remove attributes in the Hash structure * @param key External key value * @param hashKey internal key value */ void hDel(String key, Object... hashKey); /** * Determine whether the property is present in the Hash structure * @param key External key * @param hashKey internal key * @return Return whether it exists */ Boolean hHasKey(String key, String hashKey); /** * Increment of attributes in the Hash structure * @param key External key * @param hashKey internal key * @param delta incremental condition * @return Return incremented data */ Long hIncr(String key, String hashKey, Long delta); /** * Decreasing attributes in the Hash structure * @param key External key * @param hashKey internal key * @param delta incremental condition * @return Return the decremented data */ Long hDecr(String key, String hashKey, Long delta); /** * Get Set structure * @param key key * @return Return to set collection */ Set<Object> sMembers(String key); /** * Add properties to the Set structure * @param key key * @param values value set * @return Return to increase the number */ Long sAdd(String key, Object... values); /** * Add properties to the Set structure * @param key key * @param time Expiry time * @param values collection * @return Return the number added */ Long sAdd(String key, long time, Object... values); /** * Is it a property in Set * @param key key * @param value value * @return Return whether it exists */ Boolean sIsMember(String key, Object value); /** * Get the length of the Set structure * @param key key * @return Return length */ Long sSize(String key); /** * Remove properties in Set structure * @param key key * @param values value collection * @return The amount of data deleted */ Long sRemove(String key, Object... values); /** * Get properties in List structure * @param key key * @param start * @param end * @return Return the query's collection */ List<Object> lRange(String key, long start, long end); /** * Get the length of the List structure * @param key key * @return length */ Long lSize(String key); /** * Get attributes in List based on index * @param key key * @param index index * @return object */ Object lIndex(String key, long index); /** * Add properties to the List structure * @param key key * @param value value * @return The increased length */ Long lPush(String key, Object value); /** * Add properties to the List structure * @param key key * @param value value * @param time Expiry time * @return The increased length */ Long lPush(String key, Object value, long time); /** * Add properties in batches to List structure * @param key key * @param values value collection * @return The increased length */ Long lPushAll(String key, Object... values); /** * Add properties in batches to List structure * @param key key * @param time Expiry time * @param values value collection * @return The increased length */ Long lPushAll(String key, Long time, Object... values); /** * Remove attributes from List structure * @param key key * @param count Total quantity * @param value value * @return Return the length after deletion */ Long lRemove(String key, long count, Object value); /** * Add value to bitmap * @param key key * @param offset offset * @param b Status * @return Results */ Boolean bitAdd(String key, int offset, boolean b); /** * Get the value of the offset from bitmap * @param key key * @param offset offset * @return Results */ Boolean bitGet(String key, int offset); /** * Get the sum of key values of bitmap * @param key key * @return Sum */ Long bitCount(String key); /** * Get the bitmap range value * @param key key * @param limit range * @param offset start offset * @return long type collection */ List<Long> bitField(String key, int limit, int offset); /** * Get all bitmap * @param key key * @return Return as binary byte array */ byte[] bitGetAll(String key); /** * Add coordinates * @param key key * @param x x * @param y y * @param name Location name * @return Return result */ Long geoAdd(String key, Double x, Double y, String name); /** * Get the coordinate set according to the city name * @param key key * @param place * @return Coordinate Set */ List<Point> geoGetPointList(String key, Object... place); /** * Calculate the distance between two cities * @param key key * @param placeOne Location 1 * @param placeTow Location 2 * @return Return distance */ Distance geoCalculationDistance(String key, String placeOne, String placeTow); /** * Get other locations near the location attached * @param key key * @param place * @param distance Nearby range * @param limit Check a few items * @param sort sort * @return Return to a nearby collection of locations */ GeoResults<<Object>> geoNearByPlace(String key, String place, Distance distance, long limit, sort); /** * Get the hash of the location * @param key key * @param place * @return Return to the collection */ List<String> geoGetHash(String key, String... place); }
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import .*; import ; import static .DECR_BY_EXPIRE_LUA_SCRIPT; import static .INCR_BY_EXPIRE_LUA_SCRIPT; /** * Default Redis service implementation * * @author */ @RequiredArgsConstructor public class DefaultRedisServiceImpl extends AbstractRedisService { private final RedisTemplate<String, Object> redisTemplate; /** * Get all keys that match the specified expression * @param pattern expression * @return * @see <a href="/commands/keys" rel="external nofollow" rel="external nofollow" >Keys Command</a> */ @Override public Set<String> keys(String pattern) { return (pattern); } /** * Save properties * @param key key value * @param value value * @param time Time Stamp */ @Override public void set(String key, Object value, long time) { valueOps().set(key, value, time, ); } /** * Save properties * @param key key value * @param value value */ @Override public void set(String key, Object value) { valueOps().set(key, value); } /** * Get attributes * @param key key value * @return Return object */ @Override public Object get(String key) { return valueOps().get(key); } /** * Get attributes in batches from specified keys * @param keys keys * @return values list, when the value is empty, the value corresponding to the key is null * @see <a href="/commands/mget" rel="external nofollow" rel="external nofollow" >MGet Command</a> */ @Override public List<Object> mGet(Collection<String> keys) { return valueOps().multiGet(keys); } /** * Get the keys attribute in batches and return a map * @param keys keys * @return map, key and value set, when value is obtained as null, this map is not saved */ @Override public Map<String, Object> mGetToMap(Collection<String> keys) { List<Object> values = valueOps().multiGet(keys); Map<String, Object> map = new HashMap<>(()); if (values == null || ()) { return map; } Iterator<String> keysIterator = (); Iterator<Object> valuesIterator = (); while (()) { String key = (); Object value = (); if (value != null) { (key, value); } } return map; } /** * Delete attributes * @param key key value * @return Return success */ @Override public Boolean del(String key) { return (key); } /** * Batch delete attributes * @param keys key value collection * @return Return the number of deletes */ @Override public Long del(Collection<String> keys) { return (keys); } /** * Set expiration time * @param key key value * @param time Time Stamp * @return Return success */ @Override public Boolean expire(String key, long time) { return (key, time, ); } /** * Get expiration time * @param key key value * @return Return timestamp */ @Override public Long getExpire(String key) { return (key, ); } /** * Determine whether the key exists * @param key key value * @return Return */ @Override public Boolean hasKey(String key) { return (key); } /** * Increment by delta * @param key key value * @param delta delta value * @return Return the result after incrementing */ @Override public Long incr(String key, long delta) { return valueOps().increment(key, delta); } /** * Press delta to increment and set the expiration time * @param key key value * @param delta delta value * @param timeout Expiry time (unit: seconds) * @return Return the result after incrementing */ @Override public Long incrAndExpire(String key, long delta, long timeout) { return (INCR_BY_EXPIRE_LUA_SCRIPT, (key), (delta), (timeout)); } /** * Decrease by delta * @param key key value * @param delta delta value * @return Return the result after decrement */ @Override public Long decr(String key, long delta) { return valueOps().decrement(key, delta); } /** * Press delta to decrement and set the expiration time * @param key key value * @param delta delta value * @param timeout Expiry time (unit: seconds) * @return Return the result after decrement */ @Override public Long decrAndExpire(String key, long delta, long timeout) { return (DECR_BY_EXPIRE_LUA_SCRIPT, (key), (delta), (timeout)); } /** * Get the properties in the Hash structure * @param key External key value * @param hashKey internal key value * @return Return the value of the internal key */ @Override public Object hGet(String key, String hashKey) { return hashOps().get(key, hashKey); } /** * Put an attribute into the Hash structure * @param key External key * @param hashKey internal key * @param value The value of the internal key * @param time Expiry time * @return Return whether it succeeds */ @Override public Boolean hSet(String key, String hashKey, Object value, long time) { hashOps().put(key, hashKey, value); return expire(key, time); } /** * Put an attribute into the Hash structure * @param key External key * @param hashKey internal key * @param value The value of the internal key */ @Override public void hSet(String key, String hashKey, Object value) { hashOps().put(key, hashKey, value); } /** * Get the entire Hash structure directly * @param key External key value * @return Return hashMap */ @Override public Map<Object, Object> hGetAll(String key) { return hashOps().entries(key); } /** * Directly set the entire Hash structure * @param key External key * @param map hashMap value * @param time Expiry time * @return Return whether it succeeds */ @Override public Boolean hSetAll(String key, Map<String, Object> map, long time) { hashOps().putAll(key, map); return expire(key, time); } /** * Directly set the entire Hash structure * @param key External key * @param map hashMap value */ @Override public void hSetAll(String key, Map<String, ?> map) { hashOps().putAll(key, map); } /** * Remove attributes in the Hash structure * @param key External key value * @param hashKey internal key value */ @Override public void hDel(String key, Object... hashKey) { hashOps().delete(key, hashKey); } /** * Determine whether the property is present in the Hash structure * @param key External key * @param hashKey internal key * @return Return whether it exists */ @Override public Boolean hHasKey(String key, String hashKey) { return hashOps().hasKey(key, hashKey); } /** * Increment of attributes in the Hash structure * @param key External key * @param hashKey internal key * @param delta incremental condition * @return Return incremented data */ @Override public Long hIncr(String key, String hashKey, Long delta) { return hashOps().increment(key, hashKey, delta); } /** * Decreasing attributes in the Hash structure * @param key External key * @param hashKey internal key * @param delta incremental condition * @return Return the decremented data */ @Override public Long hDecr(String key, String hashKey, Long delta) { return hashOps().increment(key, hashKey, -delta); } /** * Get Set structure * @param key key * @return Return to set collection */ @Override public Set<Object> sMembers(String key) { return setOps().members(key); } /** * Add properties to the Set structure * @param key key * @param values value set * @return Return to increase the number */ @Override public Long sAdd(String key, Object... values) { return setOps().add(key, values); } /** * Add properties to the Set structure * @param key key * @param time Expiry time * @param values collection * @return Return the number added */ @Override public Long sAdd(String key, long time, Object... values) { Long count = setOps().add(key, values); expire(key, time); return count; } /** * Is it a property in Set * @param key key * @param value value * @return Return whether it exists */ @Override public Boolean sIsMember(String key, Object value) { return setOps().isMember(key, value); } /** * Get the length of the Set structure * @param key key * @return Return length */ @Override public Long sSize(String key) { return setOps().size(key); } /** * Remove properties in Set structure * @param key key * @param values value collection * @return The amount of data deleted */ @Override public Long sRemove(String key, Object... values) { return setOps().remove(key, values); } /** * Get properties in List structure * @param key key * @param start * @param end * @return Return the query's collection */ @Override public List<Object> lRange(String key, long start, long end) { return listOps().range(key, start, end); } /** * Get the length of the List structure * @param key key * @return length */ @Override public Long lSize(String key) { return listOps().size(key); } /** * Get attributes in List based on index * @param key key * @param index index * @return object */ @Override public Object lIndex(String key, long index) { return listOps().index(key, index); } /** * Add properties to the List structure * @param key key * @param value value * @return The increased length */ @Override public Long lPush(String key, Object value) { return listOps().rightPush(key, value); } /** * Add properties to the List structure * @param key key * @param value value * @param time Expiry time * @return The increased length */ @Override public Long lPush(String key, Object value, long time) { Long index = listOps().rightPush(key, value); expire(key, time); return index; } /** * Add properties in batches to List structure * @param key key * @param values value collection * @return The increased length */ @Override public Long lPushAll(String key, Object... values) { return listOps().rightPushAll(key, values); } /** * Add properties in batches to List structure * @param key key * @param time Expiry time * @param values value collection * @return The increased length */ @Override public Long lPushAll(String key, Long time, Object... values) { Long count = listOps().rightPushAll(key, values); expire(key, time); return count; } /** * Remove attributes from List structure * @param key key * @param count Total quantity * @param value value * @return Return the length after deletion */ @Override public Long lRemove(String key, long count, Object value) { return listOps().remove(key, count, value); } /** * Add value to bitmap * @param key key * @param offset offset * @param b Status * @return Results */ @Override public Boolean bitAdd(String key, int offset, boolean b) { return valueOps().setBit(key, offset, b); } /** * Get the value of the offset from bitmap * @param key key * @param offset offset * @return Results */ @Override public Boolean bitGet(String key, int offset) { return valueOps().getBit(key, offset); } /** * Get the sum of key values of bitmap * @param key key * @return Sum */ @Override public Long bitCount(String key) { return ((RedisCallback<Long>) con -> (())); } /** * Get the bitmap range value * @param key key * @param limit range * @param offset start offset * @return long type collection */ @Override public List<Long> bitField(String key, int limit, int offset) { return ((RedisCallback<List<Long>>) con -> ((), ().get((limit)).valueAt(offset))); } /** * Get all bitmap * @param key key * @return Return as binary byte array */ @Override public byte[] bitGetAll(String key) { return ((RedisCallback<byte[]>) con -> (())); } /** * Add coordinates * @param key key * @param x x * @param y y * @param name Location name * @return Return result */ @Override public Long geoAdd(String key, Double x, Double y, String name) { return geoOps().add(key, new Point(x, y), name); } /** * Get the coordinate set according to the city name * @param key key * @param place * @return Coordinate Set */ @Override public List<Point> geoGetPointList(String key, Object... place) { return geoOps().position(key, place); } /** * Calculate the distance between two cities * @param key key * @param placeOne Location 1 * @param placeTow Location 2 * @return Return distance */ @Override public Distance geoCalculationDistance(String key, String placeOne, String placeTow) { return geoOps().distance(key, placeOne, placeTow, ); } /** * Get other locations near the location attached * @param key key * @param place * @param distance Nearby range * @param limit Check a few items * @param sort sort * @return Return to a nearby collection of locations */ @Override public GeoResults<<Object>> geoNearByPlace(String key, String place, Distance distance, long limit, sort) { args = () .includeDistance() .includeCoordinates(); //Judge the sorting method if ( == sort) { (); } else { (); } (limit); return geoOps().radius(key, place, distance, args); } /** * Get the hash of the location * @param key key * @param place * @return Return to the collection */ @Override public List<String> geoGetHash(String key, String... place) { return geoOps().hash(key, place); } /** * Get Helper class that simplifies Redis data access code. * @return RedisTemplate Get */ @Override protected RedisTemplate<String, Object> getTemplate() { return redisTemplate; } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.