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

How do I write a package that I can adjust the #20

Closed
hiqsociety opened this issue Jul 26, 2019 · 7 comments
Closed

How do I write a package that I can adjust the #20

hiqsociety opened this issue Jul 26, 2019 · 7 comments
Labels
question Further information is requested

Comments

@hiqsociety
Copy link

Basically I would like to make my pre-processing of fastcache to be done so I can assign the size of the cache inside my program instead of directly on fastcache.

How do I do this?


import(
        "github.com/VictoriaMetrics/fastcache"
)
type Cache struct {
        Cache fastcache
}

func New(maxBytes int) *Cache {
        if maxBytes <= 0 {
                panic(fmt.Errorf("maxBytes must be greater than 0; got %d", maxBytes))
        }
        //      var c fastcache.Cache
        //      maxBucketBytes := uint64((maxBytes + bucketsCount - 1) / bucketsCount)
        //      for i := range c.buckets[:] {
        //              c.buckets[i].Init(maxBucketBytes)
        //      }

        Fcache = fastcache.New(maxBytes)
        return Fcache
}

func (*Cache) Set(k,v []byte) {
       //do something before fastcache.Set(k,v)
}
@valyala valyala added the question Further information is requested label Jul 26, 2019
@valyala
Copy link
Collaborator

valyala commented Jul 26, 2019

Just wrap fastcache.Cache into your struct. Something like the following:

import "github.com/VictoriaMetrics/fastcache"

type Cache struct {
        c *fastcache.Cache
}

func New(maxBytes int) *Cache {
    return &Cache{
        c: fastcache.New(maxBytes),
    }
}

func (c *Cache) Set(k,v []byte) {
    //  do something before fastcache.Set

    // Call fastcache.Set
    c.c.Set(k, v)
}

@hiqsociety
Copy link
Author

Thanks!

@hiqsociety
Copy link
Author

hiqsociety commented Jul 28, 2019

I have multiple packages trying to use this

 mycustomized_cache package

How do I share this same cache from multiple other packages?

@hiqsociety hiqsociety reopened this Jul 28, 2019
@valyala
Copy link
Collaborator

valyala commented Jul 28, 2019

Just initialize cache and store it inside mycustomized_cache package and then call wrapper functions from another packages for accessing the cache:

package mycustomized_cache

var cache = New(123456789)

// Set must be called from another packages
func Set(k, v []byte) {
    cache.Set(k, v)
}

@hiqsociety
Copy link
Author

Can I initialize the cache , var cache = mycustomized_cache.New(123456789) from

       package main

first?

then use it in other packages?

@valyala
Copy link
Collaborator

valyala commented Jul 28, 2019

Yes, but in this case you should manually pass the initialized cache to all the packages that use it.

@hiqsociety
Copy link
Author

oic. I get it thanks.

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

No branches or pull requests

2 participants