S
Sin Jeong-hun
I sometimes use the following caching scheme in order to save
unnecessary CPU usage.
class SampleClass
{
LargeClass _SampleProperty;
public LargeClass SampleProperty
{
get
{
if(_SampleProperty == null)
{
_SampleProperty = DoSomeCostlyOperation();
}
return _SampleProperty;
}
}
I think you may do the same, probably. The problem is the
_SampleProperty is kept for ever until the SampleClass instance is
destroyed, and it is acceptable that _SampleProperty is deleted.
Because if it becomes null, it can always be recreated on demand. This
scheme looks like a little bit waste. Of course, I can create my own
scheme to remove the cached _SampleProperty. For example, set a timer
in the SampleClass, so if 10 minutes have passed since the last access
of SampleProperty, it will delete _SampleProperty. But that may not be
efficient.
Is there any proven, widely used strategy for this thing? Or you just
don't care about the cached properties?
unnecessary CPU usage.
class SampleClass
{
LargeClass _SampleProperty;
public LargeClass SampleProperty
{
get
{
if(_SampleProperty == null)
{
_SampleProperty = DoSomeCostlyOperation();
}
return _SampleProperty;
}
}
I think you may do the same, probably. The problem is the
_SampleProperty is kept for ever until the SampleClass instance is
destroyed, and it is acceptable that _SampleProperty is deleted.
Because if it becomes null, it can always be recreated on demand. This
scheme looks like a little bit waste. Of course, I can create my own
scheme to remove the cached _SampleProperty. For example, set a timer
in the SampleClass, so if 10 minutes have passed since the last access
of SampleProperty, it will delete _SampleProperty. But that may not be
efficient.
Is there any proven, widely used strategy for this thing? Or you just
don't care about the cached properties?