Skip to content

Instantly share code, notes, and snippets.

View ldclakmal's full-sized avatar
🎯
Focusing

Chanaka Lakmal ldclakmal

🎯
Focusing
View GitHub Profile
@ldclakmal
ldclakmal / twitter-connector-release.yml
Created May 5, 2020 17:13
Twitter Connector - GitHub Actions
name: Deployment
on:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
@ldclakmal
ldclakmal / abstract-cache.bal
Last active June 6, 2020 14:58
Abstract Cache of Ballerina
public type AbstractCache abstract object {
public function put(string key, any value, int maxAgeInSeconds = -1) returns Error?;
public function get(string key) returns any|Error;
public function invalidate(string key) returns Error?;
public function invalidateAll() returns Error?;
public function hasKey(string key) returns boolean;
public function keys() returns string[];
public function size() returns int;
public function capacity() returns int;
};
@ldclakmal
ldclakmal / abstract-eviction-policy.bal
Created June 6, 2020 14:59
Abstract Eviction Policy of Ballerina
public type AbstractEvictionPolicy abstract object {
public function get(LinkedList list, Node node);
public function put(LinkedList list, Node node);
public function remove(LinkedList list, Node node);
public function replace(LinkedList list, Node newNode, Node oldNode);
public function clear(LinkedList list);
public function evict(LinkedList list) returns Node?;
};
@ldclakmal
ldclakmal / linked-list-api.bal
Created June 6, 2020 15:01
Linked List API of Ballerina
public type Node record {|
any value;
Node? prev = ();
Node? next = ();
|};
public type LinkedList record {
Node? head;
Node? tail;
};
@ldclakmal
ldclakmal / cache-config.bal
Created June 6, 2020 15:02
Cache Configuration of Ballerina
public type CacheConfig record {|
int capacity = 100;
AbstractEvictionPolicy evictionPolicy = new LruEvictionPolicy();
float evictionFactor = 0.25;
int defaultMaxAgeInSeconds = -1;
int cleanupIntervalInSeconds?;
|};
@ldclakmal
ldclakmal / cache-sample-1.bal
Created June 6, 2020 15:04
Cache Usage without Error Handling
_ = check cache.put("key1", "value1");
string value = <string> check cache.get("key1");
_ = check cache.invalidate("key1");
_ = check cache.invalidateAll();
boolean hasKey = cache.hasKey("key1");
string[] keys = cache.keys();
int size = cache.size();
int capacity = cache.capacity();
@ldclakmal
ldclakmal / cache-sample-2.bal
Created June 6, 2020 15:05
Cache Usage with Error Handling
cache:Error? result = cache.put("key1", "value1");
if (result is cache:Error) {
// implement what to do, if any error happen when inserting
// item to the cache
}
any|cache:Error result = cache.get("key1");
if (result is cache:Error) {
// implement what to do, if any error happen when retrieving
// item from the cache
}
@ldclakmal
ldclakmal / custom-cache.bal
Created June 6, 2020 15:05
Custom Cache of Ballerina
public type CustomCache object {
*AbstractCache;
private AbstractEvictionPolicy evictionPolicy;
private LinkedList list;
public function __init(AbstractEvictionPolicy evictionPolicy) {
self.evictionPolicy = evictionPolicy;
self.list = {
@ldclakmal
ldclakmal / custom-eviction-policy.bal
Created June 6, 2020 15:06
Custom Eviction Policy of Ballerina
public type CustomEvictionPolicy object {
*cache:AbstractEvictionPolicy;
public function get(LinkedList list, Node node) {
// custom implementation related to updating the
// `LinkedList` based on the get operation.
}
}
import ballerina/http;
import ballerina/graphql;
listener http:Listener apiGateway = new(9090,
secureSocket = {
key: {
certFile: "/path/to/public.crt",
keyFile: "/path/to/private.key"
}
}