mirror of
				https://github.com/grassrootseconomics/eth-tracker.git
				synced 2025-11-03 18:11:53 +01:00 
			
		
		
		
	feat: optimize exists to check multiple keys in one call (#40)
* closes #32
This commit is contained in:
		
							parent
							
								
									fd59d286f5
								
							
						
					
					
						commit
						f1086fcdc1
					
				
							
								
								
									
										2
									
								
								internal/cache/cache.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								internal/cache/cache.go
									
									
									
									
										vendored
									
									
								
							@ -9,7 +9,7 @@ type (
 | 
			
		||||
	Cache interface {
 | 
			
		||||
		Add(context.Context, string) error
 | 
			
		||||
		Remove(context.Context, string) error
 | 
			
		||||
		Exists(context.Context, string) (bool, error)
 | 
			
		||||
		Exists(context.Context, ...string) (bool, error)
 | 
			
		||||
		Size(context.Context) (int64, error)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										4
									
								
								internal/cache/redis.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								internal/cache/redis.go
									
									
									
									
										vendored
									
									
								
							@ -40,8 +40,8 @@ func (c *redisCache) Remove(ctx context.Context, key string) error {
 | 
			
		||||
	return c.client.Do(ctx, cmd).Error()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *redisCache) Exists(ctx context.Context, key string) (bool, error) {
 | 
			
		||||
	cmd := c.client.B().Exists().Key(key).Build()
 | 
			
		||||
func (c *redisCache) Exists(ctx context.Context, key ...string) (bool, error) {
 | 
			
		||||
	cmd := c.client.B().Exists().Key(key...).Build()
 | 
			
		||||
	res, err := c.client.Do(ctx, cmd).AsBool()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								internal/cache/xmap.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										11
									
								
								internal/cache/xmap.go
									
									
									
									
										vendored
									
									
								
							@ -26,9 +26,14 @@ func (c *mapCache) Remove(_ context.Context, key string) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *mapCache) Exists(_ context.Context, key string) (bool, error) {
 | 
			
		||||
	_, ok := c.xmap.Load(key)
 | 
			
		||||
	return ok, nil
 | 
			
		||||
func (c *mapCache) Exists(_ context.Context, key ...string) (bool, error) {
 | 
			
		||||
	for _, v := range key {
 | 
			
		||||
		_, ok := c.xmap.Load(v)
 | 
			
		||||
		if ok {
 | 
			
		||||
			return true, nil
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return false, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *mapCache) Size(_ context.Context) (int64, error) {
 | 
			
		||||
 | 
			
		||||
@ -147,16 +147,10 @@ func (hc *HandlerContainer) checkStables(ctx context.Context, from string, to st
 | 
			
		||||
		return true, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO: Pipeline this check on Redis with a new method
 | 
			
		||||
	fromExists, err := hc.cache.Exists(ctx, from)
 | 
			
		||||
	exists, err := hc.cache.Exists(ctx, from, to)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	toExists, err := hc.cache.Exists(ctx, to)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return fromExists || toExists, nil
 | 
			
		||||
	return exists, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user