Skip to content

Instantly share code, notes, and snippets.

@navferty
Last active May 30, 2024 14:54
Show Gist options
  • Save navferty/41fe7b2fc6e81ef1b225bb854370c34c to your computer and use it in GitHub Desktop.
Save navferty/41fe7b2fc6e81ef1b225bb854370c34c to your computer and use it in GitHub Desktop.

Coding tasks

Coding: Memory cache

Create a memory cache. It's main API - method GetOrAdd(key, valueFactory, token). Cache should return value by key, if the value is present. Otherwise, it should asynchronously invoke valueFactory to get value, and store value that was returned.

If second call with same key is performed, while first call is still executing, it should not perform additional valueFactory invocation.

public interface ICache<TValue>
{
    Task<TValue?> GetOrAdd(string key, Func<CancellationToken, Task<TValue>> valueFactory, CancellationToken token);
    bool TryGet(string key, out TValue? value);
    bool Remove(string key);
}

Would be great if your implementation supports:

  • TTL configuration (passing TTL options in ctor, and/or in GetOrAdd method)
  • Error handling (including different strategies to cache failures along with successfully returned values)
  • Multithreading support
  • Logging

You are allowed to add/modify the interface if needed.

Coding: HTTP calls reducer

Create a wrapper/httphandler for default HttpClient. It should reduce number of parallel request, re-using response of requests that are already in process, to calls that are requested when initial request is still being executed.

You need to think over a strategy of detecting, which requests can be "re-used". See: Idempotence.

Design: Service discovery

Design a 'service discovery' web service. It should be able to provide information about all instances of applications in network, about their statuses (healthy/degraded/down). You can provide a solution in C# code, pseudocode, diagram or just as description in words.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment