R: A rudimentary cache for large objects

ergm_state_cache {ergm} | R Documentation

## A rudimentary cache for large objects

### Description

This cache is intended to store large, infrequently changing data structures such as `ergm_model`s and `ergm_proposal`s on worker nodes.

### Usage

    ergm_state_cache(
      comm = c("pass", "all", "clear", "insert", "get", "check", "list"),
      hash,
      object
    )

### Arguments

`comm` | a character string giving the desired function; see the default argument above for permitted values and Details for meanings; partial matching is supported.
`hash` | a character string, typically a `digest()` of the object.
`object` | the object to be stored. Supported tasks are, respectively, to do nothing (the default), return all entries (mainly useful for testing), clear the cache, insert into cache, retrieve an object by hash, check if a hash is present, or list hashes defined. Deleting an entry can be accomplished by inserting a `NULL` for that hash. Cache is limited to a hard-coded size (currently 4). This should accommodate an `ergm_model` and an `ergm_proposal` for unconstrained and constrained MCMC. When additional objects are stored, the oldest object is purged and garbage-collected.

### Note

If called via, say, `clusterMap(cl, ergm_state_cache, ...)` the function will not accomplish anything. This is because `parallel` package will serialise the `ergm_state_cache()` function object, send it to the remote node, evaluate it there, and fetch the return value. This will leave the environment of the worker's `ergm_state_cache()` unchanged. To actually evaluate it on the worker nodes, it is recommended to wrap it in an empty function whose environment is set to `globalenv()`. See Examples below.

### Examples

    ## Not run:
    # Wrap ergm_state_cache() and call it explicitly from ergm:
    call_ergm_state_cache <- function(...) ergm::ergm_state_cache(...)

    # Reset the function's environment so that it does not get sent to
    # worker nodes (who have their own instance of ergm namespace
    # loaded).
    environment(call_ergm_state_cache) <- globalenv()

    # Now, call the the wrapper function, with ... below replaced by
    # lists of desired arguments.
    clusterMap(cl, call_ergm_state_cache, ...)

    ## End(Not run)

[Package ergm version 4.2.1 Index]