Ejemplo n.º 1
0
/*
 * Document-class: Cache
 *
 * <code>Cache</code> provides for a Hashtable of strings in shared
 * memory (via a memory mapped file), which thus can be shared between
 * processes on a computer.  Here is an example of its usage:
 *
 *   $lm = Cache.new :namespace => "viewcounters"
 *   $lm[:foo] = 1
 *   $lm[:foo]          # -> "1"
 *   $lm.delete(:foo)
 *
 * <code>Cache</code> can also be used as a persistent key value
 * database, just use the :filename instead of the :namespace parameter.
 *
 *   $lm = Cache.new :filename => "my-database.lmc"
 *   $lm[:foo] = 1
 *   $lm[:foo]          # -> "1"
 *   $lm.delete(:foo)
 *
 *  == Default sizes of memory pools
 *
 *  The default size for memory pools is 1024 (MB). It cannot be changed later,
 *  so choose a size that will provide enough space for all your data.  You
 *  might consider setting this size to the maximum filesize of your
 *  filesystem.  Also note that while these memory pools may look large on your
 *  disk, they really aren't, because with sparse files only those parts of the
 *  file which contain non-null data actually use disk space.
 *
 *  == Automatic recovery from crashes
 *
 *  In case a process is terminated while accessing a memory pool, other
 *  processes will wait for the lock up to 2 seconds, and will then try to
 *  resume the aborted operation.  This can also be done explicitly by using
 *  Cache.check(options).
 *
 *  == Clearing memory pools
 *
 *  Removing memory pools can be done with Cache.drop(options).
 *
 *  == Environment
 *
 *  If you use the :namespace parameter, the .lmc file for your namespace will
 *  reside in /var/tmp/Cache.  This can be overriden by setting the
 *  LMC_NAMESPACES_ROOT_PATH variable in the environment.
 *
 *  == Storing Ruby Objects
 *
 *  If you want to store Ruby objects instead of just strings, consider
 *  using Cache::SharedObjectStorage.
 *
 */
void mrb_mruby_cache_gem_init(mrb_state *mrb)
{
  struct RClass *Cache;
  lmc_init();
  Cache = mrb_define_class(mrb, "Cache", mrb->object_class);
  MRB_SET_INSTANCE_TT(Cache, MRB_TT_DATA);

  mrb_define_method(mrb, Cache, "initialize", Cache_init, MRB_ARGS_REQ(1));

  mrb_define_singleton_method(mrb, (struct RObject *)Cache, "drop", Cache__drop, MRB_ARGS_REQ(1));
  mrb_define_singleton_method(mrb, (struct RObject *)Cache, "disable_test_crash", Cache__disable_test_crash,
                              MRB_ARGS_NONE());
  mrb_define_singleton_method(mrb, (struct RObject *)Cache, "enable_test_crash", Cache__enable_test_crash,
                              MRB_ARGS_NONE());

  mrb_define_method(mrb, Cache, "get", Cache__get, MRB_ARGS_REQ(1));
  mrb_define_method(mrb, Cache, "[]", Cache__get, MRB_ARGS_REQ(1));
  mrb_define_method(mrb, Cache, "delete", Cache__delete, MRB_ARGS_REQ(1));
  mrb_define_method(mrb, Cache, "set", Cache__set, MRB_ARGS_REQ(2));
  mrb_define_method(mrb, Cache, "clear", Cache__clear, MRB_ARGS_NONE());
  mrb_define_method(mrb, Cache, "[]=", Cache__set, MRB_ARGS_REQ(2));
  mrb_define_method(mrb, Cache, "close", Cache__close, MRB_ARGS_NONE());
  mrb_define_method(mrb, Cache, "size", Cache__size, MRB_ARGS_NONE());
  mrb_define_method(mrb, Cache, "shm_status", Cache__shm_status, MRB_ARGS_NONE());
  mrb_define_method(mrb, Cache, "check_consistency", Cache__check_consistency, MRB_ARGS_NONE());
  // mrb_define_method(mrb, Cache, "keys", Cache__keys, MRB_ARGS_NONE());
  DONE;
}
Ejemplo n.º 2
0
/*
 * Document-class: LocalMemCache
 * 
 * <code>LocalMemCache</code> provides for a Hashtable of strings in shared
 * memory (via a memory mapped file), which thus can be shared between
 * processes on a computer.  Here is an example of its usage:
 *
 *   $lm = LocalMemCache.new :namespace => "viewcounters"
 *   $lm[:foo] = 1
 *   $lm[:foo]          # -> "1"
 *   $lm.delete(:foo)
 *
 * <code>LocalMemCache</code> can also be used as a persistent key value
 * database, just use the :filename instead of the :namespace parameter.
 *
 *   $lm = LocalMemCache.new :filename => "my-database.lmc"
 *   $lm[:foo] = 1
 *   $lm[:foo]          # -> "1"
 *   $lm.delete(:foo)
 *
 *  == Default sizes of memory pools
 *
 *  The default size for memory pools is 1024 (MB). It cannot be changed later,
 *  so choose a size that will provide enough space for all your data.  You
 *  might consider setting this size to the maximum filesize of your
 *  filesystem.  Also note that while these memory pools may look large on your
 *  disk, they really aren't, because with sparse files only those parts of the
 *  file which contain non-null data actually use disk space.
 *
 *  == Automatic recovery from crashes
 *
 *  In case a process is terminated while accessing a memory pool, other
 *  processes will wait for the lock up to 2 seconds, and will then try to
 *  resume the aborted operation.  This can also be done explicitly by using
 *  LocalMemCache.check(options).
 *
 *  == Clearing memory pools
 *
 *  Removing memory pools can be done with LocalMemCache.drop(options). 
 *
 *  == Environment
 *  
 *  If you use the :namespace parameter, the .lmc file for your namespace will
 *  reside in /var/tmp/localmemcache.  This can be overriden by setting the
 *  LMC_NAMESPACES_ROOT_PATH variable in the environment.
 *
 *  == Storing Ruby Objects
 *
 *  If you want to store Ruby objects instead of just strings, consider 
 *  using LocalMemCache::SharedObjectStorage.
 *
 */
void Init_rblocalmemcache() {
  lmc_init();
  LocalMemCache = rb_define_class("LocalMemCache", rb_cObject);
  rb_define_singleton_method(LocalMemCache, "_new", LocalMemCache__new2, 1);
  rb_define_singleton_method(LocalMemCache, "drop", 
      LocalMemCache__drop, 1);
  rb_define_singleton_method(LocalMemCache, "check", 
      LocalMemCache__check, 1);
  rb_define_singleton_method(LocalMemCache, "disable_test_crash", 
      LocalMemCache__disable_test_crash, 0);
  rb_define_singleton_method(LocalMemCache, "enable_test_crash", 
      LocalMemCache__enable_test_crash, 0);
  rb_define_method(LocalMemCache, "get", LocalMemCache__get, 1);
  rb_define_method(LocalMemCache, "[]", LocalMemCache__get, 1);
  rb_define_method(LocalMemCache, "delete", LocalMemCache__delete, 1);
  rb_define_method(LocalMemCache, "set", LocalMemCache__set, 2);
  rb_define_method(LocalMemCache, "clear", LocalMemCache__clear, 0);
  rb_define_method(LocalMemCache, "[]=", LocalMemCache__set, 2);
  rb_define_method(LocalMemCache, "keys", LocalMemCache__keys, 0);
  rb_define_method(LocalMemCache, "each_pair", LocalMemCache__each_pair, 0);
  rb_define_method(LocalMemCache, "random_pair", LocalMemCache__random_pair, 
      0);
  rb_define_method(LocalMemCache, "close", LocalMemCache__close, 0);
  rb_define_method(LocalMemCache, "size", LocalMemCache__size, 0);
  rb_define_method(LocalMemCache, "shm_status", LocalMemCache__shm_status, 0);
  rb_define_method(LocalMemCache, "check_consistency", 
      LocalMemCache__check_consistency, 0);

  lmc_rb_sym_namespace = ID2SYM(rb_intern("namespace"));
  lmc_rb_sym_filename = ID2SYM(rb_intern("filename"));
  lmc_rb_sym_size_mb = ID2SYM(rb_intern("size_mb"));
  lmc_rb_sym_min_alloc_size = ID2SYM(rb_intern("min_alloc_size"));
  lmc_rb_sym_force = ID2SYM(rb_intern("force"));
  rb_require("localmemcache.rb");
}