mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-10-27 05:52:29 +08:00 
			
		
		
		
	* pb * fix: Modifying other fields while setting IsPrivateChat does not take effect * fix: quote message error revoke * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * upgrading pkg tools * fix * fix * optimize log output * feat: support GetLastMessage * feat: support GetLastMessage * feat: s3 switch * feat: s3 switch * fix: GetUsersOnline * feat: SendBusinessNotification supported configuration parameters * feat: SendBusinessNotification supported configuration parameters * feat: SendBusinessNotification supported configuration parameters * feat: seq conversion failed without exiting * monolithic * fix: DeleteDoc crash * fix: DeleteDoc crash * fix: monolithic * fix: monolithic * fix: fill send time * fix: fill send time * fix: crash caused by withdrawing messages from users who have left the group * fix: mq * fix: mq * fix: user msg timestamp * fix: mq * 1 * 1 * 1 * 1 * 1 * 1 * 1 * seq read config * seq read config * 1 * 1 * fix: the source message of the reference is withdrawn, and the referenced message is deleted * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1
		
			
				
	
	
		
			64 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package mcache
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"encoding/json"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
 | |
| 	"github.com/openimsdk/tools/log"
 | |
| )
 | |
| 
 | |
| func getCache[V any](ctx context.Context, cache database.Cache, key string, expireTime time.Duration, fn func(ctx context.Context) (V, error)) (V, error) {
 | |
| 	getDB := func() (V, bool, error) {
 | |
| 		res, err := cache.Get(ctx, []string{key})
 | |
| 		if err != nil {
 | |
| 			var val V
 | |
| 			return val, false, err
 | |
| 		}
 | |
| 		var val V
 | |
| 		if str, ok := res[key]; ok {
 | |
| 			if json.Unmarshal([]byte(str), &val) != nil {
 | |
| 				return val, false, err
 | |
| 			}
 | |
| 			return val, true, nil
 | |
| 		}
 | |
| 		return val, false, nil
 | |
| 	}
 | |
| 	dbVal, ok, err := getDB()
 | |
| 	if err != nil {
 | |
| 		return dbVal, err
 | |
| 	}
 | |
| 	if ok {
 | |
| 		return dbVal, nil
 | |
| 	}
 | |
| 	lockValue, err := cache.Lock(ctx, key, time.Minute)
 | |
| 	if err != nil {
 | |
| 		return dbVal, err
 | |
| 	}
 | |
| 	defer func() {
 | |
| 		if err := cache.Unlock(ctx, key, lockValue); err != nil {
 | |
| 			log.ZError(ctx, "unlock cache key", err, "key", key, "value", lockValue)
 | |
| 		}
 | |
| 	}()
 | |
| 	dbVal, ok, err = getDB()
 | |
| 	if err != nil {
 | |
| 		return dbVal, err
 | |
| 	}
 | |
| 	if ok {
 | |
| 		return dbVal, nil
 | |
| 	}
 | |
| 	val, err := fn(ctx)
 | |
| 	if err != nil {
 | |
| 		return val, err
 | |
| 	}
 | |
| 	data, err := json.Marshal(val)
 | |
| 	if err != nil {
 | |
| 		return val, err
 | |
| 	}
 | |
| 	if err := cache.Set(ctx, key, string(data), expireTime); err != nil {
 | |
| 		return val, err
 | |
| 	}
 | |
| 	return val, nil
 | |
| }
 |