Something like this? I'm not entirely sure how you envisage this working but I'd recommend using the facilities of caption
rather than trying to create your own stuff from scratch, given that you are loading caption
already.
\documentclass{article}
\usepackage{caption,array}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{positioning}
\DeclareCaptionFormat{sidewise}{%
\begin{tikzpicture}
\node (caption text) [align=left] {%
\begin{tabular}{p{60mm}}
#3
\end{tabular}};
\node (caption label) [rotate=90, inner xsep=0pt, left=5mm of caption text.north west, font=\sffamily] {#1};
\draw [ultra thick] (caption label.south west) -- (caption label.south east);
\draw [ultra thick, orange!75!black] ([xshift=.35em]caption label.south west) -- ([xshift=.35em]caption label.south east);
\end{tikzpicture}}
\captionsetup{format=sidewise}
\begin{document}
\lipsum[1]
\begin{figure}[t!p]
\centering
\includegraphics[scale=.5]{example-image-a}
\caption{My caption can be of a variable size.}
\label{fig:mylabel}
\end{figure}%
\lipsum[2]
\begin{figure}
\centering
\includegraphics[scale=.5]{example-image-b}
\caption{{This will be a much longer caption. Hence, we need another line.}}
\label{fig:myotherlabel}
\end{figure}
\end{document}


EDIT
This is a modification of the version you posted building on my answer above. I wanted to avoid the overfull box warnings that was generating by using an overlay
TikZ picture. What I don't understand is why I cannot use a minipage
environment of width \textwidth
. But it does avoid the warnings, although I'm not sure it is the most efficient method.
Note that you need two runs for things to end up in the right places. On first compilation, things will be (apparently) randomly scattered all over the place.
\documentclass{article}
\usepackage{caption,array}
\usepackage{lipsum}
\usepackage{tikz,tikzpagenodes}
\usetikzlibrary{positioning,tikzmark,calc}
\DeclareCaptionFormat{sidewise}{%
\raggedleft%
\begin{tikzpicture}[remember picture]%
\node (caption text) [align=left] {%
\begin{minipage}{.975\textwidth}
#3
\end{minipage}};
\end{tikzpicture}%
\begin{tikzpicture}[overlay, remember picture]
\node (caption label) [rotate=90, inner xsep=0pt, left=5mm of caption text.north west, font=\sffamily] {#1};
\draw [ultra thick] (caption label.south west) -- (caption label.south east);
\draw [ultra thick, orange] ([xshift=.35em]caption label.south west) -- ([xshift=.35em]caption label.south east);
\end{tikzpicture}}
\DeclareCaptionLabelSeparator{none}{}
\captionsetup{format=sidewise,justification=justified}
\begin{document}
\begin{figure}
\centering
\includegraphics[scale=.5]{example-image-b}
\caption{{This will be a much longer caption. Hence, we need another long long long long long long long long long long long long line.}}
\label{fig:myotherlabel}
\end{figure}
\lipsum[1]
\end{document}
