You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.3 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright (c) 2004-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. using System.Collections.Generic;
  25. namespace Externals.MySql.Data.Common
  26. {
  27. internal class Cache<KeyType, ValueType>
  28. {
  29. private int _capacity;
  30. private Queue<KeyType> _keyQ;
  31. private Dictionary<KeyType, ValueType> _contents;
  32. public Cache(int initialCapacity, int capacity)
  33. {
  34. _capacity = capacity;
  35. _contents = new Dictionary<KeyType, ValueType>(initialCapacity);
  36. if (capacity > 0)
  37. _keyQ = new Queue<KeyType>(initialCapacity);
  38. }
  39. public ValueType this[KeyType key]
  40. {
  41. get
  42. {
  43. ValueType val;
  44. if (_contents.TryGetValue(key, out val))
  45. return val;
  46. else
  47. return default(ValueType);
  48. }
  49. set { InternalAdd(key, value); }
  50. }
  51. public void Add(KeyType key, ValueType value)
  52. {
  53. InternalAdd(key, value);
  54. }
  55. private void InternalAdd(KeyType key, ValueType value)
  56. {
  57. if (!_contents.ContainsKey(key))
  58. {
  59. if (_capacity > 0)
  60. {
  61. _keyQ.Enqueue(key);
  62. if (_keyQ.Count > _capacity)
  63. _contents.Remove(_keyQ.Dequeue());
  64. }
  65. }
  66. _contents[key] = value;
  67. }
  68. }
  69. }
  70. #endif