Developer Area

root/apps/outlook/branches/1.5/DotTelSystem/Lookup/Cache/LookupCache.cs @ 588

Revision 588, 7.1 kB (checked in by jonmaycock, 10 months ago)
Line 
1///<summary>
2/// Author: Ben Dowling & Nick Brunwin
3///------------------------
4///Copyright (c) 2008, Telnic Ltd.
5///
6///All rights reserved.
7///
8///Redistribution and use in source and binary forms, with or
9///without modification, are permitted provided that the following
10///conditions are met:
11///
12///*    Redistributions of source code must retain the above
13///     copyright notice, this list of conditions and the
14///     following disclaimer.
15///*    Redistributions in binary form must reproduce the above
16///     copyright notice, this list of conditions and the following
17///     disclaimer in the documentation and/or other materials
18///     provided with the distribution.
19///*    Neither the name of the Telnic Ltd. nor the names of its
20///     contributors may be used to endorse or promote products
21///     derived from this software without specific prior written
22///     permission.
23///
24///THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25///CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26///INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27///MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28///DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29///CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30///SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31///LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
32///USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33///AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34///LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35///IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36///THE POSSIBILITY OF SUCH DAMAGE.
37///--------------------------
38///</summary>
39using System;
40using System.Collections.Generic;
41using System.Text;
42using System.Collections;
43using System.IO;
44using System.Runtime.Serialization;
45using System.Runtime.Serialization.Formatters.Binary;
46using org.telnic.outlook.util;
47using org.telnic.outlook.friending;
48
49namespace org.telnic.outlook.lookup
50{
51    public class LookupCache
52    {
53        private static string PATH = PathLocation.FilePath + "DotTelCache.out";
54        public static string Path
55        {
56            get
57            {
58                return PATH;
59            }
60        }
61
62        private static Dictionary<string, LookupResult> cache = LoadCache();
63       
64        private LookupCache() { }
65
66        /// <summary>
67        /// Puts the given result in the cache for the given telname
68        /// </summary>
69        /// <param name="telname">The telname to store the cache for</param>
70        /// <param name="result">The result to cache for the given telname</param>
71        public static void SetResult(string telname, LookupResult result)
72        {
73            lock (cache)
74            {
75                if (cache.ContainsKey(telname) && !cache[telname].Changed)
76                {
77                    result.Changed = !result.Equals(cache[telname]);
78                }
79                else
80                {
81                    result.Changed = true;
82                }
83                cache[telname] = result;
84            }
85        }
86
87        /// <summary>
88        /// Sets the given telname as seen - its changes have been noted or discarded by the user
89        /// </summary>
90        /// <param name="telname">The telname to mark as seen</param>
91        public static void SetAsSeen(string telname)
92        {
93            lock (cache)
94            {
95                if (cache.ContainsKey(telname))
96                {
97                    cache[telname].Changed = false;
98                }
99            }
100        }
101
102        /// <summary>
103        /// Removes the given telname's cache from the cache.
104        /// </summary>
105        /// <param name="telname">The telname whose cache is to be removed</param>
106        public static void RemoveResult(string telname)
107        {
108            lock (cache)
109            {
110                cache.Remove(telname);
111            }
112        }
113
114        /// <summary>
115        /// Gets the cached result for the given telname or null if no entry exists
116        /// </summary>
117        /// <param name="telname"></param>
118        /// <returns></returns>
119        public static LookupResult GetResult(string telname)
120        {
121            lock (cache)
122            {
123                //Commented out because it is unnecessary
124                //string  privateTelname = Publishers.Instance.GetPrivateTelname(telname);
125               if (cache.ContainsKey(telname))
126                {
127                    return cache[telname];
128                }
129            }
130
131            return null;
132        }
133
134        private static Dictionary<string, LookupResult> LoadCache()
135        {
136            cache = new Dictionary<string, LookupResult>();
137            try
138            {
139                // try and load a stored copy of the cache
140                lock (PATH)
141                {
142                    lock (cache)
143                    {
144                        if (File.Exists(PATH))
145                        {
146                            FileStream fs = new FileStream(PATH, FileMode.Open);
147                            BinaryFormatter bf = new BinaryFormatter();
148                            Dictionary<string, LookupResult> storedCache = (Dictionary<string, LookupResult>)bf.Deserialize(fs);
149                            fs.Close();
150                            return storedCache;
151                        }
152                    }
153                }
154            }
155            catch (Exception e)
156            {
157                // Cannot access cache file, so we'll start with a blank cache
158                Logger.Log(Logger.Section.Lookups, "Problem accessing cache file: " + e.Message);
159                return new Dictionary<string, LookupResult>();
160            }
161            return new Dictionary<string, LookupResult>();
162        }
163
164        public static void SaveCache()
165        {
166            try
167            {
168                lock (PATH)
169                {
170                    lock (cache)
171                    {
172                        FileStream fs = new FileStream(PATH, FileMode.Create);
173                        BinaryFormatter bf = new BinaryFormatter();
174                        bf.Serialize(fs, cache);
175                        fs.Close();
176                    }
177                }
178            }
179            catch (Exception e)
180            {
181                Logger.Log(Logger.Section.Lookups, "Cache Serialization Error: " + e.Message);
182            }
183        }
184
185        public static void ClearCache()
186        {
187            lock (cache)
188            {
189                cache.Clear();
190            }
191            try
192            {
193                lock (LookupCache.Path)
194                {
195                    if (File.Exists(LookupCache.Path))
196                    {
197                        File.Delete(LookupCache.Path);
198                    }
199                }
200            }
201            catch (Exception e)
202            {
203                Logger.Log(Logger.Section.Lookups, "Exception clearing cache: " + e.Message);
204            }
205        }
206    }
207}
Note: See TracBrowser for help on using the browser.
Telnic
Search This Site
Partners
Neustar
ICANN
Main site | WHOIS | Sell .tel | FAQ | Archived Site | About Telnic | Contact Us