Exemple #1
0
 /// <summary>
 /// Invalidates a cached answer for a Question by removing it from the cache.
 /// Uses the lock to be thread-safe.
 /// </summary>
 /// <typeparam name="TAnswer">The answer type.</typeparam>
 /// <param name="question">The Question.</param>
 public void Invalidate <TAnswer>(ICacheableQuestion <TAnswer> question)
 {
     lock (Lock)
     {
         Cache.Remove(question);
     }
 }
Exemple #2
0
 /// <summary>
 /// Checks if the cache contains an answer for the given Question.
 /// Uses the lock to be thread-safe.
 /// </summary>
 /// <typeparam name="TAnswer">The answer type.</typeparam>
 /// <param name="question">The Question.</param>
 /// <returns></returns>
 public bool Has <TAnswer>(ICacheableQuestion <TAnswer> question)
 {
     lock (Lock)
     {
         return(Cache.ContainsKey(question));
     }
 }
Exemple #3
0
        /// <summary>
        /// Gets an answer from the cache.
        /// If the cache does not have the answer, it asks the Question using the given Actor.
        /// Uses the lock to be thread-safe.
        /// </summary>
        /// <typeparam name="TAnswer">The answer type.</typeparam>
        /// <param name="question">The Question.</param>
        /// <param name="actor">The Screenplay Actor.</param>
        /// <returns></returns>
        public TAnswer Get <TAnswer>(ICacheableQuestion <TAnswer> question, IActor actor)
        {
            lock (Lock)
            {
                if (!Cache.ContainsKey(question))
                {
                    TAnswer answer = actor.AsksFor(question);
                    Cache.Add(question, answer);
                }

                return((TAnswer)Cache[question]);
            }
        }
 /// <summary>
 /// Private constructor.
 /// </summary>
 /// <param name="question">The target Question.</param>
 private CachedAnswer(ICacheableQuestion <TAnswer> question) => Question = question;
Exemple #5
0
 /// <summary>
 /// A simplified extension method for caching answers.
 /// Calls will look like `Actor.Discovers(...)` instead of `Actor.AsksFor(CachedAnswer.For(...))`.
 /// WARNING: Do NOT cache answers to every Question.
 /// Only cache answers that you know will be fairly constant.
 /// Use this extension method only when explicitly caching answers.
 /// </summary>
 /// <typeparam name="TAnswer">The answer type.</typeparam>
 /// <param name="actor">The Actor.</param>
 /// <param name="question">The Question.</param>
 /// <returns></returns>
 public static TAnswer Discovers <TAnswer>(this IActor actor, ICacheableQuestion <TAnswer> question) =>
 GetsCached(actor, question);
Exemple #6
0
 /// <summary>
 /// A simplified extension method for caching answers.
 /// Calls will look like `Actor.GetsCached(...)` instead of `Actor.AsksFor(CachedAnswer.For(...))`.
 /// WARNING: Do NOT cache answers to every Question.
 /// Only cache answers that you know will be fairly constant.
 /// Use this extension method only when explicitly caching answers.
 /// </summary>
 /// <typeparam name="TAnswer">The answer type.</typeparam>
 /// <param name="actor">The Actor.</param>
 /// <param name="question">The Question.</param>
 /// <returns></returns>
 public static TAnswer GetsCached <TAnswer>(this IActor actor, ICacheableQuestion <TAnswer> question) =>
 actor.AsksFor(CachedAnswer <TAnswer> .For(question));