Skip to main content
`Ctrl`+`K`

# 7.25.4. `grn_cache`

## 7.25.4.1. Summary

Note

This API is experimental.

`grn_cache` is a data store that keeps responses of select command. It is not general use cache object. It is only for select command.

You can just change the current cache object by `grn_cache_current_set()`. select command response cache is done internally.

select command uses one global cache object. If you open multiple databases, the one cache is shared. It is an important problem.

If you open multiple databases and use select command, you need to use `grn_cache` object. It is groonga-httpd case. If you open only one database or don’t use select command, you don’t need to use `grn_cache` object. It is rroonga case.

## 7.25.4.2. Example

Here is an example that change the current cache object.

    grn_cache *cache;
    grn_cache *cache_previous;
    cache = grn_cache_open(ctx);
    cache_previous = grn_cache_current_get(ctx);
    grn_cache_current_set(ctx, cache);
    /* grn_ctx_send(ctx, ...); */
    grn_cache_current_set(ctx, cache_previous);

## 7.25.4.3. Reference

type grn_cache

  It is an opaque cache object. You can create a `grn_cache` by `grn_cache_open()` and free the created object by `grn_cache_close()`.

grn_cache *grn_cache_open(grn_ctx *ctx)

  Creates a new cache object.

  If memory allocation for the new cache object is failed, `NULL` is returned. Error information is stored into the `ctx`.

  Parameters:

    - ctx – The context.

  Returns:

    A newly allocated cache object on success, `NULL` otherwise. The returned cache object must be freed by `grn_cache_close()`.

grn_rc grn_cache_close(grn_ctx *ctx, grn_cache *cache)

  Frees resourses of the `cache`.

  Parameters:

    - ctx – The context.

    - cache – The cache object to be freed.

  Returns:

    `GRN_SUCCESS` on success, not `GRN_SUCCESS` otherwise.

grn_rc grn_cache_current_set(grn_ctx *ctx, grn_cache *cache)

  Sets the cache object that is used in select command.

  Parameters:

    - ctx – The context.

    - cache – The cache object that is used in select command.

  Returns:

    `GRN_SUCCESS` on success, not `GRN_SUCCESS` otherwise.

grn_cache *grn_cache_current_get(grn_ctx *ctx)

  Gets the cache object that is used in select command.

  Parameters:

    - ctx – The context.

  Returns:

    The cache object that is used in select command. It may be `NULL`.

grn_rc grn_cache_set_max_n_entries(grn_ctx *ctx, grn_cache *cache, unsigned int n)

  Sets the max number of entries of the cache object.

  Parameters:

    - ctx – The context.

    - cache – The cache object to be changed.

    - n – The new max number of entries of the cache object.

  Returns:

    `GRN_SUCCESS` on success, not `GRN_SUCCESS` otherwise.

unsigned int grn_cache_get_max_n_entries(grn_ctx *ctx, grn_cache *cache)

  Gets the max number of entries of the cache object.

  Parameters:

    - ctx – The context.

    - cache – The target cache object.

  Returns:

    The max number of entries of the cache object.