MCPcopy Create free account
hub / github.com/QuantumNous/new-api / updateUserCacheFieldAtVersion

Function updateUserCacheFieldAtVersion

model/user_auth_cache.go:237–277  ·  view source on GitHub ↗
(userId int, field string, value any, authVersion int64)

Source from the content-addressed store, hash-verified

235}
236
237func updateUserCacheFieldAtVersion(userId int, field string, value any, authVersion int64) error {
238 if !common.RedisEnabled {
239 return nil
240 }
241 if userId <= 0 || authVersion <= 0 {
242 return fmt.Errorf("invalid user auth version")
243 }
244 const script = `
245local incoming = tonumber(ARGV[1])
246local pending = tonumber(redis.call('GET', KEYS[2]) or '0')
247local committed = tonumber(redis.call('GET', KEYS[3]) or '0')
248local current = tonumber(redis.call('HGET', KEYS[1], 'AuthVersion') or '0')
249if pending > incoming or committed > incoming or current > incoming then
250 return 0
251end
252if committed < incoming then
253 redis.call('SET', KEYS[3], ARGV[1])
254end
255if pending > 0 and pending <= incoming then
256 redis.call('DEL', KEYS[2])
257end
258if redis.call('EXISTS', KEYS[1]) == 0 then
259 return 1
260end
261if current ~= incoming then
262 return 1
263end
264redis.call('HSET', KEYS[1], ARGV[2], ARGV[3], 'CacheSchema', ARGV[4])
265return 1`
266 result, err := common.RDB.Eval(context.Background(), script,
267 []string{getUserCacheKey(userId), getUserAuthFenceKey(userId), getUserAuthVersionKey(userId)},
268 authVersion, field, value, userCacheSchemaVersion,
269 ).Int()
270 if err != nil {
271 return err
272 }
273 if result == 0 {
274 return ErrUserAuthCachePending
275 }
276 return nil
277}

Calls 3

getUserCacheKeyFunction · 0.85
getUserAuthFenceKeyFunction · 0.85
getUserAuthVersionKeyFunction · 0.85