clearCache.Object.Rd
Clear fields that are defined to have cached values by assigning NULL
to these fields.
# S3 method for class 'Object'
clearCache(this, recursive=TRUE, gc=FALSE, ...)
Returns itself (invisible).
For more information see Object
.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Defining a class with a 'cached' fields
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
setConstructorS3("CachedObject", function(...) {
extend(Object(), "CachedObject",
...
)
})
setMethodS3("as.character", "CachedObject", function(this, ...) {
s <- NextMethod("as.character", this, ...)
s <- sprintf("%s RAM: %.2fkb.", s, objectSize(this)/1024)
s
})
#> NULL
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Example of clearing a cache fields, reassigning it,
# and then clearing it again
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
obj <- CachedObject(a=1, b=1:10^5, "cached:c"=1:10^6)
print(obj)
#> [1] "CachedObject: 0x600f7acc5d80"
print(ll(obj))
#> member data.class dimension objectSize
#> 1 a numeric 1 56
#> 2 b numeric 100000 400048
#> 3 c numeric 1000000 4000048
clearCache(obj, gc=TRUE)
print(obj)
#> [1] "CachedObject: 0x600f7acc5d80"
print(ll(obj))
#> member data.class dimension objectSize
#> 1 a numeric 1 56
#> 2 b numeric 100000 400048
#> 3 c NULL 0 0
obj$c <- 1:10^6
#> Error in getStaticInstance.Object(this): Cannot get static instance. Failed to locate Class object for class 'CachedObject'.
print(obj)
#> [1] "CachedObject: 0x600f7acc5d80"
print(ll(obj))
#> member data.class dimension objectSize
#> 1 a numeric 1 56
#> 2 b numeric 100000 400048
#> 3 c NULL 0 0
clearCache(obj, gc=TRUE)
print(obj)
#> [1] "CachedObject: 0x600f7acc5d80"
print(ll(obj))
#> member data.class dimension objectSize
#> 1 a numeric 1 56
#> 2 b numeric 100000 400048
#> 3 c NULL 0 0
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Clearing cached fields recursively and make sure it
# avoids race conditions due to circular dependences
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
objA <- CachedObject(a=2, "cached:c"=1:10^6, prev=NULL)
print(ll(objA))
#> member data.class dimension objectSize
#> 1 a numeric 1 56
#> 2 c numeric 1000000 4000048
#> 3 prev NULL 0 0
objB <- CachedObject(a=2, "cached:c"=1:10^6, prev=objA)
print(ll(objB))
#> member data.class dimension objectSize
#> 1 a numeric 1 56
#> 2 c numeric 1000000 4000048
#> 3 prev CachedObject 1 4000552
objC <- CachedObject(a=3, "cached:c"=1:10^6, prev=objB)
print(ll(objC))
#> member data.class dimension objectSize
#> 1 a numeric 1 56
#> 2 c numeric 1000000 4000048
#> 3 prev CachedObject 1 4001072
objA$prev <- objC
#> Error in getStaticInstance.Object(this): Cannot get static instance. Failed to locate Class object for class 'CachedObject'.
clearCache(objA, gc=TRUE)
print(ll(objA))
#> member data.class dimension objectSize
#> 1 a numeric 1 56
#> 2 c NULL 0 0
#> 3 prev NULL 0 0
print(ll(objB))
#> member data.class dimension objectSize
#> 1 a numeric 1 56
#> 2 c numeric 1000000 4000048
#> 3 prev CachedObject 1 504
print(ll(objC))
#> member data.class dimension objectSize
#> 1 a numeric 1 56
#> 2 c numeric 1000000 4000048
#> 3 prev CachedObject 1 4001072