Exemple #1
0
        /// <summary>
        /// Checks whether a given Expansion is Cached
        /// </summary>
        /// <param name="u">Uri</param>
        /// <returns></returns>
        public bool IsExpansionCached(Uri u)
        {
            if (!this._enabled) return false;

            String cachePath = Path.Combine(this._resultsDir, u.GetSha256Hash());
            return File.Exists(cachePath) && this.IsFresh(cachePath);
        }
        public void ToCache(Uri requestUri, Uri responseUri, IGraph g, String etag)
        {
            //Cache a local copy of the Graph
            try
            {
                bool cacheTwice = !requestUri.ToString().Equals(responseUri.ToString() , StringComparison.OrdinalIgnoreCase);

                if (this._canCacheGraphs)
                {
                    String graph = Path.Combine(this._graphDir, requestUri.GetSha256Hash());
                    this._ttlwriter.Save(g, graph);

                    //If applicable also cache under the responseUri
                    if (cacheTwice)
                    {
                        graph = Path.Combine(this._graphDir, responseUri.GetSha256Hash());
                        this._ttlwriter.Save(g, graph);
                    }
                }

                //Cache the ETag if present
                if (this._canCacheETag && etag != null && !etag.Equals(String.Empty))
                {
                    int id = requestUri.GetEnhancedHashCode();
                    bool requireAdd = false;
                    if (this._etags.ContainsKey(id))
                    {
                        if (!this._etags[id].Equals(etag))
                        {
                            //If the ETag has changed remove it and then re-add it
                            this.RemoveETag(requestUri);
                            requireAdd = true;
                        }
                    }
                    else
                    {
                        requireAdd = true;
                    }

                    if (requireAdd)
                    {
                        //Add a New ETag
                        this._etags.Add(id, etag);
                        using (StreamWriter writer = new StreamWriter(this._etagFile, true, Encoding.UTF8))
                        {
                            writer.WriteLine(id + "\t" + etag);
                            writer.Close();
                        }
                    }

                    //Cache under the Response URI as well if applicable
                    if (cacheTwice)
                    {
                        id = responseUri.GetEnhancedHashCode();
                        requireAdd = false;
                        if (this._etags.ContainsKey(id))
                        {
                            if (!this._etags[id].Equals(etag))
                            {
                                //If the ETag has changed remove it and then re-add it
                                this.RemoveETag(responseUri);
                                requireAdd = true;
                            }
                        }
                        else
                        {
                            requireAdd = true;
                        }

                        if (requireAdd)
                        {
                            using (StreamWriter writer = new StreamWriter(this._etagFile, true, Encoding.UTF8))
                            {
                                writer.WriteLine(id + "\t" + etag);
                                writer.Close();
                            }
                        }
                    }
                }
            }
            catch (IOException)
            {
                //Ignore - if we get an IO Exception we failed to cache somehow
            }
            catch (RdfOutputException)
            {
                //Ignore - if we get an RDF Output Exception then we failed to cache
            }
        }
        public IRdfHandler ToCache(Uri requestUri, Uri responseUri, String etag)
        {
            IRdfHandler handler = null;
            try
            {
                bool cacheTwice = !requestUri.ToString().Equals(responseUri.ToString(), StringComparison.OrdinalIgnoreCase);

                //Cache the ETag if present
                if (this._canCacheETag && etag != null && !etag.Equals(String.Empty))
                {
                    int id = requestUri.GetEnhancedHashCode();
                    bool requireAdd = false;
                    if (this._etags.ContainsKey(id))
                    {
                        if (!this._etags[id].Equals(etag))
                        {
                            //If the ETag has changed remove it and then re-add it
                            this.RemoveETag(requestUri);
                            requireAdd = true;
                        }
                    }
                    else
                    {
                        requireAdd = true;
                    }

                    if (requireAdd)
                    {
                        //Add a New ETag
                        this._etags.Add(id, etag);
                        using (StreamWriter writer = new StreamWriter(this._etagFile, true, Encoding.UTF8))
                        {
                            writer.WriteLine(id + "\t" + etag);
                            writer.Close();
                        }
                    }

                    //Cache under the Response URI as well if applicable
                    if (cacheTwice)
                    {
                        id = responseUri.GetEnhancedHashCode();
                        requireAdd = false;
                        if (this._etags.ContainsKey(id))
                        {
                            if (!this._etags[id].Equals(etag))
                            {
                                //If the ETag has changed remove it and then re-add it
                                this.RemoveETag(responseUri);
                                requireAdd = true;
                            }
                        }
                        else
                        {
                            requireAdd = true;
                        }

                        if (requireAdd)
                        {
                            using (StreamWriter writer = new StreamWriter(this._etagFile, true, Encoding.UTF8))
                            {
                                writer.WriteLine(id + "\t" + etag);
                                writer.Close();
                            }
                        }
                    }
                }

                //Then if we are caching Graphs return WriteThroughHandlers to do the caching for us
                if (this._canCacheGraphs)
                {
                    String graph = Path.Combine(this._graphDir, requestUri.GetSha256Hash());
                    handler = new WriteThroughHandler(this._formatterType, new StreamWriter(graph), true);

                    if (cacheTwice)
                    {
                        graph = Path.Combine(this._graphDir, responseUri.GetSha256Hash());
                        handler = new MultiHandler(new IRdfHandler[] { handler, new WriteThroughHandler(this._formatterType, new StreamWriter(graph), true) });
                    }
                }
            }
            catch (IOException)
            {
                //Ignore - if we get an IO Exception we failed to cache somehow
            }
            catch (RdfOutputException)
            {
                //Ignore - if we get an RDF Output Exception then we failed to cache
            }
            return handler;
        }
        /// <summary>
        /// Gets the path to the locally cached copy of the Graph from the given URI
        /// </summary>
        /// <param name="u">URI</param>
        /// <returns></returns>
        /// <remarks>
        /// This method does not do any cache expiry calculations on the file.  This is due to the fact that we'll store local copies of Graphs for which we have ETags and when using ETags we rely on the servers knowledge of whether the resource described by the URI has changed rather than some arbitrary caching duration that we/the user has set to use.
        /// </remarks>
        public String GetLocalCopy(Uri u)
        {
            if (this._canCacheGraphs)
            {
                if (this._nocache.Contains(u.GetSha256Hash())) return null;

                String graph = Path.Combine(this._graphDir, u.GetSha256Hash());
                if (File.Exists(graph))
                {
                    return graph;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// Is there a locally cached copy of the Graph from the given URI which is not expired
        /// </summary>
        /// <param name="u">URI</param>
        /// <param name="requireFreshness">Whether the local copy is required to meet the Cache Freshness (set by the Cache Duration)</param>
        /// <returns></returns>
        public bool HasLocalCopy(Uri u, bool requireFreshness)
        {
            try
            {
                if (this._canCacheGraphs)
                {
                    if (this._nocache.Contains(u.GetSha256Hash())) return false;

                    String graph = Path.Combine(this._graphDir, u.GetSha256Hash());
                    if (File.Exists(graph))
                    {
                        if (requireFreshness)
                        {
                            //Check the freshness of the local copy
                            DateTime created = File.GetCreationTime(graph);
                            TimeSpan freshness = DateTime.Now - created;
                            if (freshness > this._cacheDuration)
                            {
                                //Local copy has expired
                                File.Delete(graph);
                                return false;
                            }
                            else
                            {
                                return true;
                            }
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                //If we get an error trying to detect if a URI is cached then it can't be in the cache
                return false;
            }
        }
        /// <summary>
        /// Removes a locally cached copy of a URIs results from the Cache
        /// </summary>
        /// <param name="u">URI</param>
        public void RemoveLocalCopy(Uri u)
        {
            if (u == null) return;

            try
            {
                String graph = Path.Combine(this._graphDir, u.GetSha256Hash());
                if (File.Exists(graph))
                {
                    File.Delete(graph);
                }
            }
            catch
            {
                //If error add to the list of uncachable URIs
                this._nocache.Add(u.GetSha256Hash());
            }
        }
 /// <summary>
 /// Gets the ETag for the given URI
 /// </summary>
 /// <param name="u">URI</param>
 /// <returns></returns>
 /// <exception cref="KeyNotFoundException">Thrown if there is no ETag for the given URI</exception>
 public String GetETag(Uri u)
 {
     if (this._canCacheETag)
     {
         if (this._nocache.Contains(u.GetSha256Hash())) throw new KeyNotFoundException("No ETag was found for the URI " + u.ToString());
         int id = u.GetEnhancedHashCode();
         if (this._etags.ContainsKey(id))
         {
             return this._etags[id];
         }
         else
         {
             throw new KeyNotFoundException("No ETag was found for the URI " + u.ToString());
         }
     }
     else
     {
         throw new KeyNotFoundException("No ETag was found for the URI " + u.ToString());
     }
 }
 /// <summary>
 /// Gets whether there is an ETag for the given URI
 /// </summary>
 /// <param name="u">URI</param>
 /// <returns></returns>
 public bool HasETag(Uri u)
 {
     if (this._canCacheETag)
     {
         if (this._nocache.Contains(u.GetSha256Hash())) return false;
         return this._etags.ContainsKey(u.GetEnhancedHashCode()) && this.HasLocalCopy(u, false);
     }
     else
     {
         return false;
     }
 }
Exemple #9
0
 /// <summary>
 /// Adds an Expansion to the Cache
 /// </summary>
 /// <param name="u">Uri</param>
 /// <param name="profile">Expansion Profile Uri</param>
 /// <param name="expansion">Expansion</param>
 /// <returns></returns>
 public void Add(Uri u, Uri profile, IInMemoryQueryableStore expansion)
 {
     String cachePath = Path.Combine(this._resultsDir, profile.GetSha256Hash() + "_" + u.GetSha256Hash());
     this._writer.Save(expansion, new StreamParams(cachePath));
 }
Exemple #10
0
        /// <summary>
        /// Gets an Expansion from the Cache
        /// </summary>
        /// <param name="u">Uri</param>
        /// <param name="profile">Expansion Profile Uri</param>
        /// <returns></returns>
        public IInMemoryQueryableStore GetExpansion(Uri u, Uri profile)
        {
            String cachePath = Path.Combine(this._resultsDir, profile.GetSha256Hash() + "_" + u.GetSha256Hash());

            TripleStore store = new TripleStore();
            this._parser.Load(store, new StreamParams(cachePath));
            return store;
        }