DataService.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using IMCS.CCS.Common;
  2. using IMCS.CCS.Common.Redis;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9. using System.Text;
  10. namespace IMCS.CCS.Service.Impl
  11. {
  12. /// <summary>
  13. /// 数据服务
  14. /// </summary>
  15. public class DataService : IDataService
  16. {
  17. private readonly string _webRootPath;
  18. private string PathOrKey { get; set; }
  19. private readonly IDataStorageConfigurationService _dataStorageConfigurationService;
  20. private readonly IRedisService _redisService;
  21. public DataService(IWebHostEnvironment webHostEnvironment, IDataStorageConfigurationService dataStorageConfigurationService, IRedisService redisService)
  22. {
  23. _webRootPath = webHostEnvironment.WebRootPath;
  24. _dataStorageConfigurationService = dataStorageConfigurationService;
  25. _redisService = redisService;
  26. }
  27. public bool Init(string pathOrKey)
  28. {
  29. PathOrKey = pathOrKey;
  30. return true;
  31. }
  32. /// <summary>
  33. /// 读取数据
  34. /// </summary>
  35. /// <typeparam name="T"></typeparam>
  36. /// <returns></returns>
  37. public async Task<IEnumerable<T>> ReadDataAsync<T>()
  38. {
  39. var data = string.Empty;
  40. if (_dataStorageConfigurationService.IsUseRedis())
  41. {
  42. data = await this._redisService.Database.StringGetAsync(PathOrKey);
  43. }
  44. else
  45. {
  46. try
  47. {
  48. var path = this.GetPath();
  49. //using var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  50. //var buffer = new byte[fs.Length];
  51. //fs.Position = 0;
  52. //await fs.ReadAsync(buffer, 0, buffer.Length);
  53. //var data = Encoding.UTF8.GetString(buffer);
  54. //if (string.IsNullOrWhiteSpace(data)) return default;
  55. String content = await File.ReadAllTextAsync(path);
  56. byte[] arr = Encoding.Default.GetBytes(content);
  57. data = System.Text.Encoding.UTF8.GetString(arr);
  58. }
  59. catch (Exception)
  60. {
  61. return await ReadDataAsync<T>();
  62. }
  63. }
  64. return string.IsNullOrWhiteSpace(data) ? default : JsonConvert.DeserializeObject<IEnumerable<T>>(data);
  65. }
  66. /// <summary>
  67. /// 写入数据
  68. /// </summary>
  69. /// <returns></returns>
  70. public async Task<bool> WriteDataAsync<T>(T contents)
  71. {
  72. var json = JsonConvert.SerializeObject(contents);
  73. if (_dataStorageConfigurationService.IsUseRedis())
  74. {
  75. await this._redisService.Database.StringSetAsync(PathOrKey, json);
  76. }
  77. else
  78. {
  79. try
  80. {
  81. var path = this.GetPath();
  82. //using var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  83. //fs.Seek(0, SeekOrigin.Begin);
  84. //fs.SetLength(0);
  85. //var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(contents));
  86. //fs.Position = 0;
  87. //await fs.WriteAsync(data, 0, data.Length);
  88. await File.WriteAllTextAsync(path, json);
  89. }
  90. catch (Exception)
  91. {
  92. return await WriteDataAsync(contents);
  93. }
  94. }
  95. return true;
  96. }
  97. #region 私有
  98. private string GetPath()
  99. {
  100. if (string.IsNullOrWhiteSpace(PathOrKey)) throw new MessageBox("属性 FilePath 空对象,请使用前调用 Init 函数!");
  101. var path = _webRootPath + PathOrKey;
  102. if (!File.Exists(path))
  103. {
  104. File.Create(path);
  105. }
  106. return path;
  107. }
  108. #endregion
  109. }
  110. }