Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Implementation of Redis Cluster lua

Shulou Source: shulou.com Published: 2022-06-03 04:53:38 10月03日 Update

Don't say a word, just load the goods. A lot of communication, ha, thank you to all the gods.

.

The key point is the following paragraph:

Object object = redisTemplate.execute (redisUpdateOrderScript,// if there is a key here, add "{}" as the official website says, otherwise you will report an error. All three key must be prefixed with the same Arrays.asList (hkey, amountKey, key), / / value does not require amount.longValueExact (), price.doubleValue (), price.doubleValue ())

My own understanding is that executing a script is the same as executing hget, except that the content of the lua script is executed by Redis, but the requirements for sending commands are the same. So the above three key must be prefixed with the same prefix.

.

.

.

The business logic goes like this:

Put the 20-gear port in the Redis

1. Automatic price sorting using ordered sets (sorted set)

ZADD key 0.0354 "0.0354"

two。 Then go to hash to get the value according to the price, and the val is the order quantity of this price.

HGET key 0.0354

Java code

Add plate mouth

Public void addOrderForLua (BeforeMatchDTO model) {/ / cache invalidation redisService.remove (RedisService.getPositionKey (model.getContract ()); BigDecimal price = model.getPrice (); BigDecimal amount = model.getAmount (). Multiply (PRECISION_DOUBLE); String key = RedisKeyGen.getContractPositionZsetKey (model.getContract (), model.getDirection ()); log.info ("getContractPositionZsetKey: {}", key) String hkey = RedisKeyGen.getContractPositionHashKey (model.getContract (), model.getDirection ()); log.info ("getContractPositionHashKey: {}", hkey); String amountKey = RedisKeyGen.getContractPositionAmountKey (model.getContract (), price.stripTrailingZeros (). ToPlainString ()); log.info ("getContractPositionAmountKey: {}", amountKey); log.info ("addOrderForLua contract: {}, value: {}", model.getContract (), amount.longValueExact ()) Object object = redisTemplate.execute (redisUpdateOrderScript, Arrays.asList (hkey, amountKey, key), amount.longValueExact (), price.doubleValue (), price.doubleValue (); log.info ("addOrderForLua" + object);}

Reducing plate opening

Public void subOrderForLua (String contract,BigDecimal price,BigDecimal amount,int direction) {/ / cache invalidation redisService.remove (RedisService.getPositionKey (contract)); String key = RedisKeyGen.getContractPositionZsetKey (contract, direction); log.info ("getContractPositionZsetKey: {}", key); String hkey = RedisKeyGen.getContractPositionHashKey (contract, direction); log.info ("getContractPositionHashKey: {}", hkey) String amountKey = RedisKeyGen.getContractPositionAmountKey (contract,price.stripTrailingZeros (). ToPlainString ()); log.info ("getContractPositionAmountKey: {}", amountKey); log.info ("subOrderForLua contract: {}, value: {}", contract, amount.doubleValue ()); BigDecimal amountTag = amount.multiply (PRECISION_DOUBLE). Negate (); / / negative Object nowAmount = redisService.hmGet (hkey, price.toPlainString ()) Log.info ("subOrderForLua nowAmount: {}, direction: {}", nowAmount, direction); Object object = redisTemplate.execute (redisUpdateOrderScript, Arrays.asList (hkey, amountKey, key), amountTag.longValueExact (), price.doubleValue (), price.doubleValue (); log.info ("subOrderForLua" + object);}

Query (focus on the value, please ignore the conversion)

Public List query (String contract,int direction) {List result = new ArrayList (); String key = RedisKeyGen.getContractPositionZsetKey (contract, direction); log.info ("getContractPositionZsetKey: {}", key); String hkey = RedisKeyGen.getContractPositionHashKey (contract, direction); log.info ("getContractPositionHashKey: {}", hkey); Set objectSet = null / sell from low to high if (QuotationConstants.DIRECTION_SELL = = direction) {objectSet = redisService.rangeByIndex (key, 0,19);} else {/ / buy from high to low objectSet = redisService.reverseRangeByIndex (key, 0,19);} if (objectSet! = null & & objectSet.size () > 0) {Integer [] digits = convertService.getContractDigitsForInt (contract) For (Object obj: objectSet) {log.info ("query class: {}, val: {}", obj.getClass (), JSON.toJSONString (obj)); BigDecimal price = new BigDecimal (obj.toString ()); String amountKey = RedisKeyGen.getContractPositionAmountKey (contract,price.stripTrailingZeros (). ToPlainString ()); Object object = redisService.hmGet (hkey, amountKey) Log.info ("getContractPositionAmountKey hmGet key: {}, val: {}", amountKey,object); BigDecimal valTemp = getBigDecimal (object); if (valTemp.compareTo (BigDecimal.ZERO) = = 0) continue; BigDecimal val = valTemp.divide (PRECISION_DOUBLE); QuotationOrderRsgResp resp = new QuotationOrderRsgResp (); resp.setContract (contract) Resp.setDirection (direction); resp.setPrice (convertService.setScale (price, digits [0])); resp.setVolume (convertService.setScale (val,digits [1])); resp.setTotal (convertService.setScale (price.multiply (val), add (digits [0], digits [1])); result.add (resp) }} else {log.info ("query redis is null! Contract: {}, direction: {} ", contract, direction);} return result;}

Key generation

Public static final String getContractPositionZsetKey (String contract,int direction) {return "{POSITION:" + contract+ "} .Posiion-ORDER-" + contract+ "-" + direction;} public static final String getContractPositionHashKey (String contract,int direction) {return "{POSITION:" + contract+ "} .Posiion-ORDER-VAL-" + contract+ "-" + direction;} public static final String getContractPositionAmountKey (String contract,String amount) {return "{POSITION:" + contract+ "}." + amount;}.

Lua script

Local val1 ='"''local valAmount = redis.call (' hget',KEYS [1], KEYS [2]) if not valAmount then redis.pcall ('hset',KEYS [1], KEYS [2], ARGV [1]) if tonumber (ARGV [1]) > 0 then local val2 = val1. ARGV [3].. Val1 return redis.pcall ('ZADD', KEYS [3], tonumber (ARGV [2]), val2) else return 1 endelse local tagAmount = tonumber (valAmount) + ARGV [1] redis.pcall (' hset',KEYS [1], KEYS [2], tagAmount) local val2 = val1.. ARGV [3].. Val1 local zset = redis.pcall ('ZRANK', KEYS [3], val2) if tagAmount

Tags: Price opening script prefix that is cache focus order consistency silence business word code content just command place god appearance negative number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux Redmi macOS OPPO Reno