Coravel簡介
Coravel幫助開發人員快速啟動並運行他們的.NET Core應用程序,而不會影響代碼質量。
通過為您提供簡單,富有表現力和直截了當的語法,它使高級應用程序功能易於訪問和使用。
安裝
<code> dotnetadd
package coravel 直接搜索coravel/<code>
特性
- 任務調度
通常,您必須通過Windows Task Scheduler配置cron作業或任務才能運行單個或多個重複發生的任務。
使用Coravel,您可以使用簡單,優雅,流利的語法在一個地方設置所有計劃的任務-代碼!
- 隊列
Coravel為您提供了一個零配置隊列,該隊列在內存中運行,以將繁重的任務卸載到後臺,而不是讓用戶等待其HTTP請求完成!
- 緩存
Coravel為您提供了一個易於使用的API,用於在.NET Core應用程序中進行緩存。
默認情況下,它使用內存中的緩存,但也具有數據庫驅動程序以實現更可靠的方案!
- 事件廣播
Coravel的事件廣播可幫助您構建各部分之間松耦合的可維護應用程序!
- 郵件
電子郵件並不像應該的那樣容易。幸運的是,Coravel提供了以下解決方案:
1、內置電子郵件友好的razor模板
2、簡單靈活的郵件API
3、呈現電子郵件以進行視覺測試
4、支持SMTP,本地日誌文件或BYOM(“自帶郵件”)驅動程序的驅動程序
5、通過appsettings.json進行快速簡單的配置
任務調度
配置
<code> services.AddScheduler() var provider = app.ApplicationServices; provider.UseScheduler(scheduler => { scheduler.Schedule(()
=> Console.WriteLine("Every minute during the week."
) ) .EveryMinute() .Weekday(); }); scheduler.Schedule(()
=> Console.WriteLine("Hourly on Mondays."
) ) .Hourly() .Monday();僅在星期一每小時運行一次任務。 scheduler.Schedule(()
=> Console.WriteLine("Daily at 1 pm."
) ) .DailyAtHour(13
);//
Or .DailyAt(13
,00
)/<code>
<code>public
class
SendDailyReportsEmailJob
:IInvocable
{private
IMailer _mailer;private
IUserRepository _repo;public
SendDailyReportsEmailJob
(IMailer mailer, IUserRepository repo
) {this
._mailer = mailer;this
._repo = repo; }public
async
TaskInvoke
() {var
users =await
this
._repo.GetUsersAsync();foreach
(var
userin
users) {var
mailable =new
NightlyReportMailable(user);await
this
._mailer.SendAsync(mailable); } } } scheduler .Schedule() .Daily();/<code>
隊列
<code># ConfigureServices() 添加 services.AddQueue(); # 注入 IQueue _queue;public
HomeController(IQueue queue) {this
._queue = queue; } # 異步this
._queue.QueueAsyncTask(async() => { await Task.Delay(1000
); Console.WriteLine("This was queued!"
); }); # 同步public
IActionResult QueueTask() {this
._queue.QueueTask(() => Console.WriteLine("This was queued!"
));return
Ok(); } # 事件廣播this
._queue.QueueBroadcast(new OrderCreated(orderId)); # 取消隊列var
(taskGuid, token) = queue.QueueCancellableInvocable(); token.Cancel(); # 後臺隊列public
class
SendWelcomeUserEmailInvocable
:IInvocable
,IInvocableWithPayload
<UserModel
> {public
UserModel Payload {get
;set
; }public
async Task Invoke() { } }var
userModel = await _userService.Get(userId); queue.QueueInvocableWithPayload(userModel);/<code>
緩存
<code> services.AddCache();private
ICache _cache;public
CacheController
(ICache cache
) {this
._cache = cache; }string
BigDataLocalFunction
() {return
"Some Big Data"
; };this
._cache.Remember("BigDataCacheKey"
, BigDataLocalFunction, TimeSpan.FromMinutes(10
));async
SomeTypeBigDataLocalFunctionAsync
() {return
await
SomeCostlyDbCall(); };await
this
._cache.RememberAsync("BigDataCacheKey"
, BigDataLocalFunctionAsync, TimeSpan.FromMinutes(10
));bool
hasKey =await
this
._cache.HasAsync("BigDataCacheKey"
);string
bigDataValue =await
this
._cache.GetAsync<string
>("BigDataCacheKey"
);this
._cache.Flush();this
._cache.Forget("BigDataCacheKey"
); services.AddSQLServerCache(connectionString); services.AddPostgreSQLCache(connectionString);1
、繼承Coravel.Cache.Interfaces.ICache2
、services.AddCache(new
RedisCache()); /<code>
總結
Coravel個人覺得很適合在微服務中使用。
源碼地址:
https://github.com/jamesmh/coravel,有興趣的同學可以去研究。
最後希望大家多多評論、關注、點贊、轉發,你們的支持,是我更新下去的最大動力。