https://github.com/techascent/tech.resource
RAII resource management system
https://github.com/techascent/tech.resource
Last synced: about 1 year ago
JSON representation
RAII resource management system
- Host: GitHub
- URL: https://github.com/techascent/tech.resource
- Owner: techascent
- Created: 2018-10-14T22:46:11.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-08-27T18:58:25.000Z (almost 3 years ago)
- Last Synced: 2024-10-28T13:40:32.046Z (over 1 year ago)
- Language: Clojure
- Size: 119 KB
- Stars: 31
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# tech.resource
[](https://clojars.org/techascent/tech.resource)
[API Documentation](https://techascent.github.io/tech.resource/)
Generic thread-safe and exception-safe non-gc or 'off heap' resource management.
There is some more information on our [blog](http://techascent.com/blog/generalized-resource-management.html).
```clojure
;;create the file
(defn new-random-file
[fname]
(resource/track (RandomAccessFile. fname) #(.close item)))
;;Use it
(resource/stack-resource-context
(let [f (new-random-file fname)]
...
))
;;Similar to with-open, this will close the file regardless
;;of what happens. The advantage is this can map to anything
;;you can implement the resource protocol with meaning network sockets,
;;JNI pointers, GPU contexts, etc. It is also possible to have the
;;gc track something if the dispose functionality does not reference
;;the item itself.
(let [f (resource/track (double-array [1 2 3]) #(println "disposed") :gc)]
...)
;;Disposed will print when the gc determines the double array is no longer
;;reachable
```
There are now explicit methods to create either a weak or soft reference.
For the differences, see
[here](https://stackoverflow.com/questions/299659/whats-the-difference-between-softreference-and-weakreference-in-java).
* [gc reference documentation and implementation](https://github.com/techascent/tech.resource/blob/master/src/tech/resource/gc.clj).
* [stack documentation and implementation](https://github.com/techascent/tech.resource/blob/master/src/tech/resource/stack.clj).
### Usage
Checkout the [stack](test/tech/resource_test.clj) and
[gc](test/tech/gc_resource_test.clj) tests.
Or take a giant leap and check out [tvm-clj](https://github.com/techascent/tvm-clj).
Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version. Complements of Tech Ascent, LLC.