Exemple #1
0
 public IHttpActionResult GetTAF([FromBody] SearchArgs args)
 {
     try {
         return(Ok(args.Search <TAF>()));
     } catch (Exception ex) {
         return(InternalServerError(ex));
     }
 }
        private Document[] Search(SearchArgs args, out bool fromCache)
        {
            Document[] list = null;
            fromCache = false;

            // Compute the hash and check if it exists
            var hash = args.MD5Sum();

            var appData = GetAppData();

            foreach (var f in appData.GetFiles())
            {
                if (f.LastWriteTime < DateTime.Now.AddDays(-1))
                {
                    f.Delete();
                }
            }

            var file = new FileInfo(Path.Combine(appData.FullName, hash + ".json"));

            if (file.Exists)
            {
                var found = CachedSearch <Document[]> .FromFile(file);

                if (found.IsValid)
                {
                    // Mark the respnse as cached and return those documents.
                    fromCache = true;
                    list      = found.Value;
                }
                else
                {
                    // Remove the results if no longer valid.
                    file.Delete();
                }
            }

            if (list == null)
            {
                list = args.Search();

                if (list.Length != 0)
                {
                    var cached = new CachedSearch <Document[]>()
                    {
                        Value     = list,
                        MD5Sum    = args.MD5Sum(),
                        Timestamp = list.First().Timestamp
                    };
                    cached.Write(file);
                }
            }

            return(list);
        }