Skip to content
This repository

MembaseClient inherits from MemcachedClient, so all operations are the same, the only difference is in the creation.

    MembaseClient client = new MembaseClient(configuration, bucketName);

The configuration can come either from app.config or can be a custom IMembaseClientConfiguration implementation. The bucketName must be a preconfigured bucket name from the cluster.

If the pools are configured in the app.config, and we do not have any buckets, we can just do

MembaseClient client = new MembaseClient();

This will connect us to the "default" bucket, and the client is ready to do the work:

client.Store(StoreMode.Set, "Test", "Hello World");

Important! Just like the MemcachedClient, this is a "heavy object", as in creating and initializing the client is quite expensive. (It connects to the pools, discovers the buckets, starts the socket pools, etc.) So, DO NOT create a client every time you want to perform an operation (well, you can, but 1) you need to Dispose() it after you finished, and it will 2) considerably slow down your application).

Do not do this:

Never create a client multiple times. Or if you have a very good reason, at least use the using(...){ } construct to release the resources.

    private Profile GetProfile(string user)
    {
        MembaseClient client = new MembaseClient("profileBucket");
        Profile p = client.Get(user) as Profile;

        if (p == null) 
            ....
    }

This is how you should do it:

Create the cache as early as possible while your application is initializing, then share the instances amongst as many parts/modules as you can. Most applications have a cache layer, this is a good candidate for doing the initialization and for exposing it to the other parts of the application.

    public static MembaseClient ProfileCache;
    public static MembaseClient OtherCache;

    public void AppInit()
    {
        ProfileCache = new MembaseClient("profileBucket");
        OtherCache = new MembaseClient("something_else");
    }

    private Profile GetProfile(string user)
    {
        Profile p = ProfileCache.Get(user) as Profile;

        if (p == null) 
            ....
    }
Something went wrong with that request. Please try again.