Пример #1
0
    public string GetWebPage(string address)
    {
        lock (this.ThisLock)
        {
            // <-- Can assume cache is valid.
            if (this.cachedAddress == address)
            {
                return(this.cachedWebPage);
                // <-- Must guarantee that cache is valid because
                // the operation returns and releases the lock.
            }
            // <-- Must guarantee that cache is valid here because
            // the operation releases the lock.
        }

        string webPage = slow.GetWebPage(address);

        lock (this.ThisLock)
        {
            // <-- Can assume cache is valid.

            // <-- Cache is no longer valid because the operation
            // changes one of the values.
            this.cachedAddress = address;
            this.cachedWebPage = webPage;
            // <-- Cache is valid again here.

            // <-- Must guarantee that cache is valid because
            // the operation releases the lock.
        }

        return(webPage);
    }
Пример #2
0
    public string GetWebPage(string address)
    {
        // <-- Can assume that cache is valid.
        if (this.cachedAddress == address)
        {
            return(this.cachedWebPage);
        }

        // <-- Must guarantee that the cache is valid, because
        // the operation can be called again before we return.
        string webPage = slow.GetWebPage(address);

        // <-- Can assume cache is valid.

        // <-- Cache is no longer valid because we are changing
        // one of the values.
        this.cachedAddress = address;
        this.cachedWebPage = webPage;
        // <-- Cache is valid again here.

        return(this.cachedWebPage);
        // <-- Must guarantee that cache is valid because we are returning.
    }