github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb/cache.go (about)

     1  // Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package leveldb
     6  
     7  // #include <stdint.h>
     8  // #include <leveldb/c.h>
     9  import "C"
    10  
    11  // Cache is a cache used to store data read from data in memory.
    12  //
    13  // Typically, NewLRUCache is all you will need, but advanced users may
    14  // implement their own *C.leveldb_cache_t and create a Cache.
    15  //
    16  // To prevent memory leaks, a Cache must have Close called on it when it is
    17  // no longer needed by the program. Note: if the process is shutting down,
    18  // this may not be necessary and could be avoided to shorten shutdown time.
    19  type Cache struct {
    20  	cache *C.leveldb_cache_t
    21  }
    22  
    23  // NewLRUCache creates a new Cache object with the capacity given.
    24  //
    25  // To prevent memory leaks, Close should be called on the Cache when the
    26  // program no longer needs it. Note: if the process is shutting down, this may
    27  // not be necessary and could be avoided to shorten shutdown time.
    28  func NewLRUCache(capacity int) *Cache {
    29  	return &Cache{C.leveldb_cache_create_lru(C.size_t(capacity))}
    30  }
    31  
    32  // Close deallocates the underlying memory of the Cache object.
    33  func (c *Cache) Close() {
    34  	C.leveldb_cache_destroy(c.cache)
    35  }