internal void ReturnExampleToCache(VowpalWabbitCachedExample <TExample> example) { if (this.exampleCache == null) { throw new ObjectDisposedException("VowpalWabbitSerializer"); } // if we reach the cache boundary, dispose the oldest example if (this.exampleCache.Count > this.settings.MaxExampleCacheSize) { var enumerator = this.exampleCache.GetEnumerator(); // this.settings.MaxExampleCacheSize is >= 1 enumerator.MoveNext(); var min = enumerator.Current; while (enumerator.MoveNext()) { if (min.Value.LastRecentUse > enumerator.Current.Value.LastRecentUse) { min = enumerator.Current; } } this.exampleCache.Remove(min.Key); min.Value.UnderlyingExample.Dispose(); } }
/// <summary> /// Serialize the example. /// </summary> /// <param name="example">The example to serialize.</param> /// <returns>The serialized example.</returns> /// <remarks>If TExample is annotated using the Cachable attribute, examples are returned from cache.</remarks> public IVowpalWabbitExample Serialize(TExample example) { if (this.exampleCache == null) { return(this.serializer(example)); } VowpalWabbitCachedExample <TExample> result; if (this.exampleCache.TryGetValue(example, out result)) { result.LastRecentUse = DateTime.UtcNow; } else { result = new VowpalWabbitCachedExample <TExample>(this, this.serializer(example)); this.exampleCache.Add(example, result); } return(result); }