Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterations by indexes #67

Closed
michail-vestnik opened this issue Apr 10, 2022 · 1 comment
Closed

Iterations by indexes #67

michail-vestnik opened this issue Apr 10, 2022 · 1 comment

Comments

@michail-vestnik
Copy link

Hi.

For example:

set("username:victoria", "value")
set("username:metrics", "value")
...

scan("username:*", 10) // last 10

Is it possible to add this much-needed feature?

If not, please advise how to iterate the data without losing high speed and quality Fastcache?

Create a slice (like an index) with bucket indexes or keys or xxhashes, iterate all elements of the slice and Get values?

Thanks

@michail-vestnik
Copy link
Author

Ok, i write this solution:

type collection struct {
	Name string
	Keys []string
}

type cache struct {
    buckets   [bucketCount]bucket
    collections []collection
}

c.collections = make([]collection, 0) // in New()

func (c *cache) Collection(name string) {
    coll := &collection{
        Name: name,
        Keys: make([]string, 0),
    }
    c.collections = append(c.collections, *coll)
}

func (c *cache) Set(collection string, k string, v []byte) error {
    var j int
    for index, coll := range c.collections {
        if coll.Name == collection {
            j = index
	}
    }
    c.collections[j].Keys = append(c.collections[j].Keys, k)
    h := xxhash.Sum64([]byte(k))
    idx := h % bucketCount
    return c.buckets[i].Set([]byte(k), v, h)
}

func (c *cache) Group(collection string, count uint) ([]string, error) {
    var (
        g []string
	j int
    )
    for index, coll := range c.collections {
        if coll.Name == collection {
	    j = index
	}
    }
    for _, k := range c.collections[j].Keys[:count] {
	v, e := c.Get(k)
	if e != nil {
		return nil, e
	}
	g = append(g, string(v))
    }
    return g, nil
}

Will this approach slow down the caching a lot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant