Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.AccessControl;
- using System.Security.Policy;
- using System.Text;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Security.Cryptography;
- using System.Collections;
- using System.Threading.Tasks;
- namespace creditinfogroup_export.BLL
- {
- public class Cacher : IDisposable
- {
- readonly Hashtable Hashes = Hashtable.Synchronized(new Hashtable());
- private const int hashLen = 16;
- private const string filename = "cache.txt";
- private int count = 0;
- const int threads = 2;
- readonly byte[] globalBuffer;
- public Cacher()
- {
- try
- {
- using (var sr = new FileStream(filename, FileMode.OpenOrCreate, FileSystemRights.Read, FileShare.Read, 1024*100, FileOptions.SequentialScan))
- {
- Hashes = Hashtable.Synchronized(new Hashtable((int)sr.Length / hashLen + 10000));
- int length = (int)sr.Length;
- globalBuffer=new byte[length];
- int sum = 0;
- while ((count = sr.Read(globalBuffer, sum, length - sum)) > 0)
- sum += count;
- }
- var tf = new TaskFactory();
- var taskQueue = new Queue<Task>();
- var tasks = new Task[threads];
- Action<object > action = readHashFile;
- for (int i = 0; i < threads; i++ )
- {
- var t = Task.Factory.StartNew(action,i);
- taskQueue.Enqueue(t);
- }
- Task.WaitAll(taskQueue.ToArray());
- }
- catch
- {
- Hashes = Hashtable.Synchronized(new Hashtable());
- }
- }
- private void readHashFile(object thread)
- {
- var buff = new byte[hashLen];
- var part = (int)thread;
- var hashesCount = (globalBuffer.Length / hashLen);
- var hashesForThread = (int)Math.Floor(hashesCount / (decimal)threads);
- var start = hashesForThread * hashLen*part;
- var end = start + (hashesForThread * hashLen);
- if (end + hashLen >= globalBuffer.Length)
- end = globalBuffer.Length;
- var sb = new StringBuilder();
- for (int i = start; i < end-hashLen; i+= hashLen)
- {
- Buffer.BlockCopy(globalBuffer,i,buff,0,hashLen);
- count++;
- Hashes.Add(Convert.ToBase64String(buff), true);
- }
- }
- public void Add(object obj)
- {
- try
- {
- Hashes.Add(ToCacheHash(obj.ToString()), true);
- }
- catch { }
- }
- public bool Exists (object o)
- {
- if (Hashes.ContainsKey(ToCacheHash(o.ToString())))
- return true;
- else
- return false;
- }
- #region IDisposable Members
- public void Dispose()
- {
- try
- {
- if (this.count < Hashes.Count)
- {
- using (
- var sr = new FileStream(filename, FileMode.Create, FileSystemRights.Modify, FileShare.None,
- 1024*10, FileOptions.SequentialScan))
- {
- foreach (string hash in Hashes.Keys)
- {
- var bytes = Convert.FromBase64String(hash);
- sr.Write(bytes, 0, bytes.Length);
- }
- sr.Flush();
- sr.Close();
- }
- }
- }
- catch
- {
- }
- }
- #endregion
- byte[] ToHash (string s, HashAlgorithm h, int bytesLength)
- {
- byte[] data = System.Text.Encoding.ASCII.GetBytes(s);
- var ret = new byte[bytesLength];
- data = h.ComputeHash(data);
- for (int i = 0; i < Math.Min(data.Length, bytesLength); i++)
- ret[i] = data[i];
- return ret;
- }
- string ToCacheHash(string s)
- {
- if (s == null)
- s = string.Empty;
- var result = new byte[hashLen];
- var sha = ToHash(s, new System.Security.Cryptography.SHA1Managed(), hashLen-5);
- var crc = ToHash(s, new Crc32(), 4);
- sha.CopyTo(result, 0);
- crc.CopyTo(result, 11);
- result[hashLen-1] = (byte)(s.Length%255);
- return Convert.ToBase64String(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement